Programming · Both

Programming Constructs

Almost every program is built from sequence, selection and iteration. Know when to use each.

The three constructs

• **Sequence** – steps in order • **Selection** – IF / ELSE decisions • **Iteration** – loops (count-controlled FOR; condition-controlled WHILE) Variables store values that can change. Use meaningful names.

Operators

• Arithmetic: + − * / MOD DIV • Comparison: = ≠ < > ≤ ≥ • Boolean: AND OR NOT Comparison operators drive selection and loop conditions.

Worked example

When is a WHILE loop better than FOR?

Solution: When you do not know in advance how many times to repeat – you loop until a condition becomes false.

Practice questions

1. Name the three basic programming constructs.

2. FOR vs WHILE?