/en-tutor-prep-ib-cs-programming-constructs-sequence-selection-loops-functions/
/en-tutor-prep-ib-cs-programming-constructs-sequence-selection-loops-functions/
General Inquiry: info@TutorZone.com.hk
Office Hours: Monday to Saturday - 9:00 AM to 6:00 PM
WhatsApp Monday to Sunday - 24 hours
Hotline: +852 6828 1809
Instant Support: +852 9061 3106

IB Computer Science: Programming Constructs — Tutor Lesson Plan (sequence, selection, loops, functions; 120 visitors fill 5 hours exactly)

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.

Objectives — the student can (1) explain how instruction order changes the output: adding before reading a price gives 0 while the correct order gives 108; (2) name a cause and a fix for infinite loops, deadlock and wrong output; (3) write a three-level if/else if/else ladder for half price, which applies only to members who are students or seniors, giving 9, 18, 4 and 32 for four visitors, 63 in total; (4) choose between a counted and a conditional loop: 5 hourly totals use a counted loop, while a water level rising from 18% to 30% at 2% a minute needs 6 passes; (5) trace a nested loop over 5 hours and 3 gates, 15 checks, with 120 entries, a mean of 24.0 and a peak of 31 in hour 3; (6) split a 12-line routine into three functions and explain why a missing global leaves revenue at 0 instead of 108.

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

Open with a pool turnstile: 5 visitors, 2 adults, 2 students and 1 seniors, tickets at 32, 18 and 8, so the correct total is 108.
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

Open with a single if: 50 marks passes, 70 marks merits. Students write two independent ifs, so a 75 prints both pass and merit; the correct form is an if/else if/else ladder where only one branch runs. Ask for the difference between else if and a second if: the ladder excludes the cases already matched, the second if does not.
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

Start with the two loop types. A counted loop with for adds 5 hourly figures, so it runs exactly 5 times. A conditional loop with while raises a water level from 18% by 2% a minute until it reaches 30%, which takes (30 minus 18) divided by 2, or 6 passes and 6 minutes. Ask when each is right: a known count uses for, an unknown count that stops on a value uses while, provided something changes each pass.
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
Sanity check (teach this): Five sanity checks. (1) The three counts must add to the head count: 2 plus 2 plus 1 is 5, and the ticket lines must add to 108. (2) The five hours must add to 120, exactly the cap of 120, so hour 6 admits nobody; answering that 12 more fit means the cap was ignored. (3) The mean of 24.0 must sit between the lowest 18 and the highest 31; outside that range the sum is wrong. (4) A condition must point the same way as the words: topping up until 30% written as greater than does nothing at all. (5) The three return values must still add to 108; if the figure changes, a block went missing. The commonest misjudgement is treating “no error” as “correct”: a bad order still runs and a plus typed as minus still finishes, so only a hand check or test data catches either.

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)

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.

Join as a tutor or coach — academic, music, sports, arts, STEM. Free to register. See how it works →