Jump to content

Is there a more efficient way to write a long disjunction ?


Recommended Posts

ElseIf (QI == 4 || QI == 5 || QI == 6 || QI == 7 || QI == 8 || QI == 9 || QI == 10 || QI == 11 || QI == 12 || QI == 14 || QI == 15 || QI == 16 || QI == 17 || QI == 18 || QI == 19 || QI == 20 || QI == 21 || QI == 22 || QI == 23)


There has to be a nicer way to write this. Something like, "Elseif (QI IsOneOf [4,5,6,7,8])" or "Elseif (QI isOneOf[4..12,14..19]).

I suppose if ((QI >= 4) && (QI <= 12)) || ((QI >= 14) && (QI <= 23)) or similar would work.

Are there related syntax moves that would be better ?

Here's another even uglier one:

If (csbmagellanquest04locations.isrunning() && SavedText[0] != CorrectReplacementText && SavedText[1] != CorrectReplacementText && SavedText[2] != CorrectReplacementText && SavedText[3] != CorrectReplacementText && SavedText[4] ......

If the above goes to index 19, that's just a monstrosity. 

Is there a better way to tackle this ?

Edited by csbx
Link to comment
Share on other sites

How about:

ElseIf QI >= 4 && QI <= 23 && QI != 13

I use a lot of this construct as well:

ElseIf myComplexCondition( args...)

bool function myComplexCondition( params...) global
	if something
		return False
	elseif somethingelse
		return False
	elseif someotherthing
		return True
	else
		return somesimplecondition
	endif
endfunction

 

Link to comment
Share on other sites

For your second example, since you are working with an array, use Find to get the index with the matching value and then do something.

If csbmagellanquest04locations.isrunning()
  Int index = SavedText.Find(CorrectReplacmentText)
  Debug.Trace("CorrectReplacementText has been found at index "+index+" of the SavedText array.")
  If index >= 0 ;a match has been found
    ;do something since they match
  Else
    ;do smething else since there was no match
  EndIf
EndIf

 

Link to comment
Share on other sites

It's really helpful watching others formulate things in ways you'd never think of. And--yes--I've never used Find for indexes so will get right on integrating that.

Cheers, you two !

Link to comment
Share on other sites

Just thought I'd offer another alternative, there's always more than one way to go about things.
you could do 2 if statements

if QI >= 4 && QI <= 12
    if QI >= 14 && QI <= 23


or this: 

if QI >=4 && QI <= 23 && QI != 13

 

Link to comment
Share on other sites

Generally if you have a condition that long, you'll want to split it up on multiple lines to make it easier to read:

if (csbmagellanquest04locations.isrunning() && \
    SavedText[0] != CorrectReplacementText && \
    SavedText[1] != CorrectReplacementText && \
    SavedText[2] != CorrectReplacementText && \
    SavedText[3] != CorrectReplacementText && \
    SavedText[4] ...... )

 

In this example, you could also do it using a loop:

bool matchFound = false
While (i < SavedText.Length && !matchFound)
    if (SavedText[i] == CorrectReplacementText)
        matchFound = true
    endif
    index += 1
EndWhile

if (csbmagellanquest04locations.isrunning() && !matchFound)
	....

But as mentioned above, using Find is the best way to do this particular example.

You could even do it in one line:

If (csbmagellanquest04locations.isrunning() && SavedText.Find(CorrectReplacementText) < 0)

 

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...