IB Computer Science: Programming Constructs — Tutor Lesson Plan (sequence, selection, loops, functions; 120 visitors fill 5 hours exactly)
- 2026-11-28
Students can write an if and a for loop but cannot say why moving one line breaks the program. Programming constructs are about control flow: the same instructions succeed or fail on order, conditions and loop boundaries.
01 Prerequisites (3-minute check)
If any of these fails, fix it before the lesson starts.
| Skill | Quick check | Pass mark |
|---|---|---|
| Assignment | total starts at 0 and the program adds 32, 18 and 8, what is total? | 58 |
| Conditions | what operator joins under 18 and over 65? | or |
| Loop counts | how many times does for i in range(4) run? | 4 times, i from 0 to 3 |
02 Lesson flow (60 minutes)
| Time | Stage | Tutor does | Student does |
|---|---|---|---|
| 0-7 min | Opening check | does changing the order break it? | write an answer first |
| 7-20 min | Order and three structural faults | 5 visitors, two orders compared | write both outputs |
| 20-34 min | Selection and Boolean operators | three price tiers, four visitors 63 | write the if ladder |
| 34-46 min | Counted and conditional loops | 5 hours, level 6 passes | write both loops |
| 46-56 min | Functions, modularisation and scope | three functions, missing global 0 | split the code, label scope |
| 56-60 min | Exit check | three questions marked on the spot | work alone |
03 Teaching script (read it out)
Scene 1: Instruction order: the same lines, a different answer
Show the wrong order first: total = total + price runs before price is read from the ticket type, so three unassigned prices are added and the total is 0. Show a second bad order: multiply the counts first and change a price afterwards, and the figure no longer matches. Ask for one sentence: a program runs top to bottom, so a line using a variable before it is set is wrong.
Then the three faults that come from order. First, an infinite loop: the while body forgets i = i + 1, the condition never becomes false and the program never stops. The fix is to name the variable that changes every pass before writing the loop, then add a guard of 10,000 passes that raises an error instead of running all night. Second, deadlock: 2 processes each lock one resource and then ask for the other, 2 resources wait on each other and both stop forever; the fix is one agreed order in which every process takes its locks. Third, wrong output: the program finishes with no error at all but a plus typed as minus turns 108 into 92, and only a hand calculation or test data catches it.
Ask the class to pair each fault with how it is noticed: an infinite loop by its run time, a deadlock by a program that stops moving, wrong output by a mismatch with the worked answer. Those three sentences are the skeleton of the Paper 2 question on why a program does not terminate.
Student task: rewrite the three badly ordered routines in the correct order and note, for each moved line, what the output becomes if it goes back.
Scene 2: Selection: turn rules into a ladder
Then bring Boolean operators in. Write half = member and (student or senior); the brackets cannot be dropped because and binds tighter than or. Substitute four visitors in turn: a member student pays 9, a non-member student pays 18 at full price, a member senior pays 4, and a member adult pays 32 because the rule does not reach adults. The four total 63. Make the class do the third one by hand: half of 8 is 4, not 4.
Cover the six relational operators: less than, less than or equal, greater than, greater than or equal, equal, not equal. The commonest exam slip is equal written with one sign, which is assignment: no error, the condition is simply always true. Add that floating point values must never be compared with an equal sign, only with a difference below a small tolerance.
Finish with a rule to translate: free entry under 120 cm or under 3 years old, but only with an adult. Students write if height is at most 120 or age is at most 3, wrapped in an outer check that a guardian exists. The question tests layering conditions, not syntax recall.
Student task: write the half-price rule as one condition, then list the price for four different visitors, one of whom deliberately fails the rule.
Scene 3: Loops and functions: count the passes, name the scope
Then put a condition inside the loop: of 18, 25, 31, 22 and 24 visitors, count only the hours above 25, which gives 1 hour, the peak of 31 in hour 3. The commonest error is placing the condition outside the loop, so only the last value is tested.
Boundaries come third: the list holds 5 items at indices 0 to 4, so while i is at most 5 reads index 5 on pass 6 and raises IndexError, while for i in range(5) runs exactly 5 times. One equals sign apart, and the most common lost mark in Paper 2.
Close on functions and scope. Split the 12-line routine into count_adults, count_students and count_seniors, leaving the main program to add three return values of 64, 36 and 8 for a total of 108. Each part can be tested alone and a rename touches one place. A global REVENUE changed inside a function without the global keyword stays 0 outside, because the function wrote a fresh local. Ask for one sentence: returning a value is safer than editing a global, and the function can then be reused for another event.
Student task: turn the five-hour total into total_visitors(hours), use it for the mean, and write one sentence on why that function needs no global.
04 Question types and worked steps
| Type | Example | Steps | Answer |
|---|---|---|---|
| Predict the output of a bad order | 5 visitors, total added before prices are read | Three steps: mark each variable value line by line, look for a use before assignment, then compare with the correct order | bad order 0; correct order 108 |
| Write a selection ladder | half price for members who are students or seniors, full price otherwise | Three steps: put the narrowest condition on top, exclude it with else if on each level, and use a final else for everything left | 9, 18, 4 and 32, total 63 |
| Counted or conditional loop | 5 hourly totals; level 18% to 30% | Two questions: is the count known in advance, use for; does it stop on a value, use while and change something each pass | for 5 passes; while 6 passes |
| Trace a nested loop | 5 hours by 3 gates | Three steps: multiply outer by inner, record the running total, and check nothing is counted twice | 15 checks, 120 entries |
| Split into functions and state scope | a 12-line routine into three functions | Three steps: find a block that can be tested alone, define parameters and a return value, then label which variables are global and which live only inside | returns 64, 36 and 8; total 108 |
05 Independent practice (3 questions)
| Question | Answer | What to watch |
|---|---|---|
| 2 adults, 2 students and 1 seniors at 32, 18 and 8. (a) What is the total? (b) If the senior price rises by 4, what is the new total? | (a) 108; (b) 112 | a price change hits only the seniors, not everyone |
| A level of 18% rises 2% a minute to reach 30%. (a) How many minutes? (b) What if the stop test were greater than 30%? | (a) 6 minutes; (b) it would reach 32%, one pass too far | a greater-than test overshoots the target |
| A list of 5 items is read with while i is at most 5. (a) Which pass fails? (b) What is the correct test? | (a) pass 6, where i is 5; (b) while i is below 5, or a for loop | indexing starts at 0, so the last index is length minus one |
06 Common mistakes and what to say
| Mistake | Student writes | Say this |
|---|---|---|
| Using one equals sign for a comparison | writing if score = 100: | one sign assigns, so the condition is always true and every visitor pays half. The four prices would collapse from [9, 18, 4, 32] to free entry. A comparison needs two signs. |
| A while loop that never updates its counter | while i is below 5 with no i = i + 1 | the condition never turns false and the program never stops. Before writing any while, name the variable that changes each pass, and add a guard of 10,000 passes so it errors instead of running overnight. |
| A second if where else if belongs | a 75 prints both pass and merit | the second if does not exclude the case the first already matched, so one student can enter two branches. A ladder uses else if and runs exactly one branch. |
| Forgetting the return value | a function totals the figures but returns nothing | without a return the caller receives None and the addition fails. Either return the value or edit the global deliberately, but returning is far safer. |
07 Homework (paper, section and question numbers)
- IB Computer Science chapter index — the three practice questions on this page
- Programming fundamentals: variables, substrings, exceptions — prerequisite: variables, scope and trace tables
- Data structures: arrays, lists, stacks and queues — prerequisite: list traversal and indexing, on which these loops build
08 Exit check (last 5 minutes)
| Question | Answer | Decision |
|---|---|---|
| Order | what does adding before reading a price give? | 108 means the order was missed |
| Selection | four prices [9, 18, 4, 32], total 63 | a half-price adult means the rule is wrong |
| Loops | 18% to 30% needs 6 passes | answering 5 means a boundary slip |
09 Tutor FAQ
Does IB use Python or Java?
Each school picks one and Paper 2 accepts either. Three differences only: Java writes else if, Python writes elif; Java uses && and ||, Python uses and and or; Java writes a three-part for. The concepts are identical.
How much deadlock detail is needed?
B2.3.1 asks only that you know the fault and how to avoid it; no thread code is required. Three sentences suffice: two processes hold one resource each, each waits for the other to release, and neither ever proceeds. The fix is one agreed acquisition order, or a timeout and retry.
How far does modularisation go?
As far as functions with parameters and a return value, plus the benefits: reuse, independent testing, one place to change, and clear interfaces when a team splits the work. Classes are B3 and not required here.
Find a tutor — freeTutor Registration →
A TutorZone lesson-prep guide. Written by the TutorZone team; questions and lesson flow are original and are not taken from any school or publisher material.



