B2.3 · Book pp. 311–332
Programming constructs
Every program is built from three ideas: do this, then that; choose; repeat. Functions then let you package those ideas for reuse.
What you need to know
- B2.3.1 Construct programs with the correct sequence of instructions.
- B2.3.2 Construct programs using selection structures.
- B2.3.3 Construct programs using looping structures.
- B2.3.4 Construct functions and modularization.
B2.3.1 Sequence
Instructions run once, in the order written. The same statements in another order can give a wrong result or a logic error.
B2.3.2 Selection
Selection decides which statements run, based on a condition. Forms include IF, IF-ELSE, ELSE IF and a nested IF, which is one IF inside another.
Use == to compare, as = assigns. In Python, indentation shows which statements belong to the IF. Java uses curly brackets.
✏️ Book correction: never use eval on input
The book reads True or False from the user with eval(member). eval runs whatever the user types as Python code, so it is a security risk, and it crashes on anything else. Compare the text instead, as above: input().lower() == "yes".
Try it: what is the output for 80, no?
You pay 80.0. The value is below 100, so the else branch runs.
B2.3.3 Loops
| Loop | Also called | Use when | Runs |
|---|---|---|---|
| Count-controlled | FOR | The number of repeats is known | As many as the count |
| Pre-condition | WHILE | Repeats depend on a condition, checked first | Possibly zero |
| Post-condition | REPEAT-UNTIL | Condition checked last | At least one |
🎯 Exam tip
Name loops as count-controlled, pre-condition or post-condition. Just saying “a FOR loop” is not enough.
A WHILE loop suits unknown repetition, such as validation (is the input sensible?) and verification (does it match the source, as when typing a password twice?).
B2.3.4 Functions and modularization
A function returns a value, while a procedure performs a task and returns none. Both make code reusable, and let a team build the parts separately. This makes code maintainable: clear, and easy to change.
Scope
A variable’s scope is where it can be used. A local variable exists only inside its function, and a global one exists across the program. Prefer locals, since a global can be changed from anywhere, causing side effects.
✏️ Book corrections
- The book says scope defines a variable’s lifetime. They differ: scope is where a name is visible, and lifetime is how long the value exists. A global’s scope can be hidden inside a function by a local of the same name, yet it still exists.
- It says globals must be declared global whenever used in a function. In Python that is needed only to assign to them. Reading needs no declaration.
- It treats a loop counter as local to the loop. This is true in Java, but in Python
istill exists after the loop ends.
✍️ Challenge
Quick quiz
Exam-style questions
These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.
-
Distinguish between pre-condition and post-condition loops, with one use of each. [3]
Mark points
- Pre-condition checks before the body, so it may run zero times [1].
- Post-condition checks after, so it runs at least once [1].
- For example, reading until a sentinel value, and showing a menu [1].
-
Construct a function isPerfect(n) that returns True if n equals the sum of its proper divisors. [4]
Mark points
- Set total to 0 [1].
- Loop d from 1 to n − 1 [1].
- If n % d == 0, add d to total [1].
- Return total == n [1].
-
Construct code that repeatedly asks for a password until two entries match. [3]
Mark points
- Input both entries [1].
- While they differ, ask again [1].
- Output a success message after the loop [1].
-
Outline one difference between a function and a procedure. [2]
Mark points
- A function returns a value [1].
- A procedure performs an action and returns no value [1].
-
Outline three ways to make code maintainable. [3]
Mark points
- Use meaningful identifiers [1].
- Split the work into functions [1].
- Add comments and consistent formatting [1].