Using Selection in Smart Pascal
Much of the power of computing comes from the ability to select which piece of code to run. This page describes two ways of performing selections. The if-then-else syntax is so close to the English language that it is easy to use. The case statement is a neat way of making a decision between several pieces of code. It is an alternative to using multiple if statements.
You will find very many examples of the effective use of if and case statements in student programs.
If StatementsThis simple if statement is taken from program Motion1:
if MobX = 0 then GoingRight := True;
In the same program there is also an example of if-then-else:
if GoingRight = True then inc(MobX) //increment MobX (increase it by 1) else dec(MobX); //decrement MobX (decrease it by 1)
Note that there is no semicolon before else, because if-then-else is considered to be a single statement.