Programming Control Structures

The code below is Pseudocode.

Sequence

Sequence is each statement after another.

x = 0
OUTPUT ("Hello world")

are examples.

Selection

When a decision needs to be made we use a selection.  There are three.

One-way (if then)
IF x > 1
    OUTPUT ("x is greater than 1")
END IF
Two-way (if then else)
IF (x > 1) THEN
     OUTPUT ("x is greater than 1")
ELSE
     OUTPUT ("x is not greater than 1")
END IF
Multi-way (case, nested if)
CASE x OF 
> 1 : OUTPUT ("x is greater than 1")
     == 1 : OUTPUT ("x equals 1")
     < 1 : OUTPUT ("x is less than 1")
END CASE

In Python you can only test if something is equal to, not greater or less than. Pseudocode you can do what you want.

or

IF x > 1 THEN
     OUTPUT ("x is greater than 1")
ELSE 
    IF x == 1 THEN
         OUTPUT ("x equals 1")
    ELSE 
         OUTPUT ("x is less than 1")
    END IF
END IF

Iteration

When you need to repeat something in code we use iteration.  There are two types of iteration.

Pre test (while)
x = 10
WHILE x > 0 DO
    OUTPUT ("x equals ", x)
    x = x - 1
END WHILE
Pre test (for)
FOR x = 10 to 0
     OUTPUT ("Counting down x ", x)
END FOR
Post test
x = 10
REPEAT
    OUTPUT ("x equals ", x)
    x--
UNTIL x <= 0

Note: There is no post test loop in Python.

Note the use of x–, this decreases the variable x by 1, it is shorthand for x = x – 1.  x++ would increase the variable x by 1.

Important: There are reasons you would choose a pre or post test iteration – a pre test may never execute, but a post test will always execute at least once.