Best practice for sequential IF statements

BlitzMax Forums/BlitzMax Beginners Area/Best practice for sequential IF statements

blackwater(Posted 2012) [#1]
Hi all. I run into the problem quite often and I was wondering if there is a better way of doing it. Here is a quick example:

method Example
if A > B
' do stufff and now B is a large number
endif


if B > C
' this executes but I don't want it to
endif

end method

In this example, when I use several IF statements like this based on conditions, what happens is the next IF state executes because of the one before it changes a value it depends on it. The only way I know how to deal with this is putting in a return statement so once it executes the rest of the method won't execute.

Is there a better way of doing this besides using a return statement?

Last edited 2012


Floyd(Posted 2012) [#2]
That's why ElseIf exists.

Print
Print "Notice that n is changed to 2, but n = 2 clause is bypassed."
Print


n = 1		' Try 2 and verify that "two" is printed.

If n = 0 
	Print "zero"
ElseIf n = 1 
	Print "one"
	n = 2
ElseIf n = 2 
	Print "two"
End If



blackwater(Posted 2012) [#3]
Hmm ok didn't realize a nested else if was the way to go, thanks!


col(Posted 2012) [#4]
You also have the Select..Case to choose..

Local Value = 10

Select Value
	Case 10
		'Do something
	Case 20
		'Do something else
End Select


You can use Select to detect a true situation too
Select True
	Case A>B
		'Do something
	Case B>C
		'Do something else
End Select


I tend to use Select..Case where there are quite a few decision paths to choose from and If..ElseIf..Endif when there are fewer.