← IB Computer Science

B2.3 · Book pp. 311–332

Programming constructs

Your level

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.

Next: sequence →

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.

🔬 Lab: put these in order

Tap in the right order, to print the average of three marks.

Next: selection →

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.

Next: loops →

B2.3.3 Loops

LoopAlso calledUse whenRuns
Count-controlledFORThe number of repeats is knownAs many as the count
Pre-conditionWHILERepeats depend on a condition, checked firstPossibly zero
Post-conditionREPEAT-UNTILCondition checked lastAt least one

🎯 Exam tip

Name loops as count-controlled, pre-condition or post-condition. Just saying “a FOR loop” is not enough.

🔬 Lab: what does range give?

 

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?).

 
 

Next: functions →

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 i still exists after the loop ends.

✍️ Challenge

 

Next: review →

Quick quiz

1. Sequence means instructions run…
2. What does elif mean in Python?
3. Which loop always runs at least once?
4. Which loop might not run at all?
5. What does list(range(2, 10, 3)) give?
6. Which is true of a function?
7. In add(x, y) called as add(3, 4), 3 and 4 are…
8. Why prefer local variables?
9. A condition that stops a loop is a…

Exam-style questions

These are written for this site in the style of Paper 2. Answer on paper first, then open the mark points.

  1. 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].
  2. 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].
  3. 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].
  4. 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].
  5. 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].