What if you wanted to check whether a given condition is met? For instance, you have a for
loop and you want to know, when the counter reached a specified value. You can do that easily with the if
command:
for ♥i from 1 to 5
if ♥i==3
dialog ‴Three was reached‴
end
dialog ♥i
end
The if
command is a block command: when a condition is met — the ♥i
counter equals 3 in this particular case — the code between the if
and the end
commands is executed. If a condition is not met, the code in the if
…end
block is skipped.
In the script above the for
loop counts from 1 to 5, and in its each iteration the robot checks, if the ♥i
counter equals 3. If it does, the message “Three was reached” is displayed in a dialog box. If it doesn’t, the robot does what it would do anyway, regardless of the condition test result: displays the current counter value.
Note: When setting up a condition that equals a given value, remember to put two equals signs and no spaces (if ♥i==3
) — otherwise, the spaces will be treated as separators between another sets of arguments and their values.
In the next lesson you’ll see how to implement alternative conditions.