Pascal Control Structures

There are three types of pascal program flow control:Sequential, selective and repetitive control structures.

Sequential:
This is where program instructions are executed from the first to the last, from top to bottom one after the other.

Example: program calculate(output); var x, y, z: integer; begin x := 5; y := 7; writeln('We are going to add x to y'); writeln('Our result will be stored in z'); z := y + x; writeln('Our Z is =', z); end.

Selection
This is where the statements to be executed are determined by the result of the condition.

There are four types of selective control structures, namely; IF .. Then, IF...THEN ...ELSE, NESTED IF AND CASE.

SIMPLE IF
This statement has only one option in that the statements within it can only be executed when the condition evaluates to true, otherwise nothing happens.

Syntax: Example:

IF...THEN...ELSE
This statement has two parts, the true and false parts and therefore has two options in that if the condition evaluates to true then the statements with the if part are executed otherwise the else part is executed.

It however does not work well with multistage (multiple) conditions.

Syntax: Example:

NESTED IF
This is where one or more IF statements are written inside another IF statement. They are said to be nested. It works well with programs that require many conditions at different lavels.

Syntax: Example:

CASE
It is a multiple branching conditional control structure (statement) that allows some particular groups of statements to be chosen from several available groups, and the selection is based upon the current value of an expression.

Syntax: CASE expression OF labellist1: statement 1; labellist2: statement 2; END

Repetitive / Iterative
These are control structures (statements) that continuously execute and can only terminate when a specified condition is fulfilled examples are; for, while..do and repeat..until.

FOR
This is a pre-condition control structure whose statements are executed as long as the condition (Boolean expression) evaluates to true otherwise it terminates. The condition is first tested before the statements are executed (it is a pre-condition control structure). The minimum number of times it can execute is zero.

N/B: The value of a counter / index should not be changed within the FOR loop.

Syntax: FOR counter:= initialvalue to finalvalue DO statement;

FOR counter:= finalvalue DOWNTO initialvalue DO statement;

WHILE...DO
This is a pre-condition control structure whose statements are executed as long as the condition (Boolean expression) evaluates to true otherwise it terminates. The condition is first tested before the statements are executed. The minimum number of times it can execute is zero.

This type of loop is also referred to as an entry controlled loop statement.

REPEAT...UNTIL
The condition is evaluated only after the statements have been executed (it is a post condition control structure). The statements will continue to be executed as long as the condition evaluates to false. The loop terminates when the condition evaluates to true.

The minimum number of times that the loop can execute is one.

Jump Statements
They course the program to skip other code (leave the normal flow) and skip to execute statements in another part of the program e.g. the GoTo statement.

Goto Statement
It is the only unconditional statement (relative to future improvements). It may be used to move out of several layers of nesting or a loop.

Syntax: Goto label;{where label may be numeric or alphanumeric} (* The label is declared above the const declaration *) Example: program trial; label 90; const pie = 3.1428; var area, radius; begin radius := 10; goto 90; 90: area := pie * radius^2; end