The elseif keyword provides you with a way of building up more complex if statements, letting you test for multiple conditions.
score as integer = 1
do if score = 0 print ( "the score is 0" ) elseif score = 1 print ( "the score is 1" ) endif
sync ( ) loop
The program will initially check if the score is 0. This condition is false, therefore the program checks the elseif, which checks if the score is 1. This condition is true so the string "the score is 1" gets printed out on screen.
When using if and elseif you can have multiple statements between the conditional checks. You cannot use the then keyword and place statements on one line.
The following example shows a score variable that gets continually incremented. A number of if statements are used to check the current range of the value. Based on its value a character will be displayed on screen.
score as integer = 1
do print ( score )
score = score + 1
if score >= 0 and score < 50 print ( "a" ) endif
if score >= 50 and score < 100 print ( "b" ) endif
if score >= 100 and score < 150 print ( "c" ) endif
if score >= 150 and score < 200 print ( "d" ) endif
if score >= 200 and score < 250 print ( "e" ) endif
sync ( ) loop
As the program runs it will check the condition for the first if statement. Then it will check the condition for the second if statement and for the third, fourth and fifth. This process is repeated as the do loop cycles round, so we end up with 5 if statements being checked continually. Using elseif is a way of ensuring your program performs less work. The same program written using elseif is far more efficient.
score as integer = 1
do print ( score )
score = score + 1
if score >= 0 and score < 50 print ( "a" ) elseif score >= 50 and score < 100 print ( "b" ) elseif score >= 100 and score < 150 print ( "c" ) elseif score >= 150 and score < 200 print ( "d" ) elseif score >= 200 and score < 250 print ( "e" ) else print ( "f" ) endif
sync ( ) loop
When this program runs the first if statement is evaluated. Initially this condition of the score being greater than or equal to 0 and less than 50 is true, therefore "a" will get printed out on screen. Given the elseif keyword has been used the remaining checks to determine the value of score are ignored. Instead of continually checking the conditions of 5 if statements only 1 is being checked initially. As time passes and the score becomes 50 or higher then the first if statement is false, so the first elseif is checked and if it's true all the remaining conditions are ignored. When score becomes higher than 250 then 5 if conditions are being checked just like with the first example, however, less code has had to be run in order to get to this stage.
The advantage of elseif is that it allows you to reduce the amount of conditional checks that are made in your program and this ultimately benefits the performance of your program. Where possible it's better to use if and elseif to perform checks instead of having lots of individual if statements.