For Beginners

Lesson 20/23

What Else?

An ifend block is not limited to testing just one condition. You can also test other conditions within the same block, using the else if and the else commands.

Run this script:

for ♥i from 1 to 5
  if ♥i==1
    dialog ‴One - to rule them all‴
  else if ♥i==2
    dialog ‴Two - to rule them, too‴
  else if ♥i==3
    dialog ‴Three - you will see‴
  else if ♥i==4
    dialog ‴Four - you were looking for‴
  else
    dialog ‴Five - time to jive‴
  end
end

As you can see, the flow is simple and straightforward: if the first condition is met, a dialog box with “One - to rule them all” message appears. If it’s not, then the second condition (else if) is tested — and so forth, until there is no other condition to test (you are left with only one option handled in the else block: the ♥i variable equals 5).

The rule of the else if and the else commands is: If the first condition after the if command is not met, the robot will start checking conditions in the else if commands that follow — the first one that is met will cause execution of its command block and leaving the ifend block.

If no else if condition is met, the robot will execute the command block between the else and the end commands or will simply leave the ifend block if no else command is provided.

In the next lesson you will learn how to check the user input.