Statements and control structures (selection and iteration)

Each line of code which performs an action is a statement.

When a statement tests if something is true or false it is a control structure.  Here are some examples – note they are checking different things, but ultimately they are testing if something is true or false.

Selections

One way selection

IF (a > b) THEN
  OUTPUT ("a is greater than b")
END IF

Two way selection

IF (a > b) THEN
  OUTPUT ("a is greater than b")
ELSE
  OUTPUT ("a is not greater than b")
END IF

Multi way selection (nested If)

IF (a > b) THEN
  OUTPUT ("a is greater than b")
ELSE IF (a == b) THEN
        OUTPUT ("a equals b")
     ELSE
        OUTPUT ("a is less than b")
     END IF
END IF

Multi way selection (Case)

CASE a OF
  > b : OUTPUT ("a is greater than b")
  == b : OUTPUT ("a equals b")
  < b : OUTPUT ("a is less than b")
END CASE

With multi way selection CASE is preferred due to its simplified code, but it is not always practical in real-world situations.

Iterations

Pre test

We use a pre test for loop when we know beforehand just how many times we need an iteration to occur.

FOR i = 1 TO 5
  OUTPUT (i)
END FOR

Result:
> 1
> 2
> 3
> 4
> 5

Pre test

We use a pre test while loop to make sure we need to start the iteration in the first place.

i = 1

WHILE i <= 5 DO
 OUTPUT (i)
 i = i + 1
END WHILE

Result:
> 1
> 2
> 3
> 4
> 5

Post test

We use the post test iteration when we know we must loop at least once, but then we can start checking to see if we need to loop anymore. There is no post test iteration in Python.

i = 1

REPEAT
  OUTPUT (i)
  i = i + 1
UNTIL i > 5

Result:
> 1
> 2
> 3
> 4
> 5