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 Statements

This 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)
   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.

Case Statements

This example of a case statement with an integer selector is taken from program AStarJS:

case mCode of
  27 : Application.Terminate;
  45 : insertButton := True;
  46 : deleteButton := True;

The selector can also be a string, as demonstrated in a console program.

Programming - a skill for life!

How to learn the Pascal language the fun way by making games in Smart Mobile Studio