Teacher Guide: Programming Constructs
Student page: Programming Constructs IB Syllabus: B2.3 Suggested timing: 5–7 periods (90 min each)
Contents
Sub-page Guides
Each sub-page below has its own dedicated teacher guide with period-by-period lesson plans, answer keys for all practice exercises, differentiation strategies, and integration notes.
| Sub-page | Syllabus | Student Page | Teacher Guide |
|---|---|---|---|
| Selection (if/else) | B2.3.2 | Student page | Teacher guide |
| Iteration (Loops) | B2.3.3 | Student page | Teacher guide |
| Methods & Scope | B2.3.4–5 | Coming soon | Coming soon |
Teaching Sequence
Selection (B2.3.2)
Progression — plain English → flowchart → code:
Before any code, write a scenario on the board: “A student scored 75. What grade should they get?” Ask a student to describe the decision process out loud. Transcribe their words: “If it’s 90 or above, grade 7. If it’s 80 or above, grade 6…”
That English description IS the algorithm. Then draw a flowchart with diamond decision boxes. Then show the Java — each if block maps directly to one branch of the flowchart. The code should feel like transcription, not invention.
Key teaching point: Java evaluates conditions top to bottom and takes the first true branch. Students often write conditions in wrong order (e.g., checking score >= 40 before score >= 90). Run a test with score = 95 — show it prints “Grade 3” instead of “Grade 7”. This demonstrates why order of conditions matters.
String comparison in conditions: Reinforce here that if (name == "Alice") does not work as expected. Always use .equals(). Students will use String conditions before OOP is fully explained — establish the rule now.
Loops (B2.3.3)
Motivation before introducing any loop syntax:
Ask: “How would you print the numbers 1 to 100 without a loop?” After a moment of silence or laughter, ask: “What are you repeating? What changes each time? What stays the same?” Those three questions map exactly to the three parts of a for loop header — initialise, condition, update. Write the header last, after establishing what each part answers.
Loop choice — use a decision guide:
| Situation | Loop |
|---|---|
| I know exactly how many iterations | for |
| I repeat while a condition holds; might run zero times | while |
| I must run at least once (e.g. menus, input validation) | do-while |
while vs do-while distinction: Ask — “What if count starts at 0 — does the while body run?” (No.) “Does the do-while body run?” (Yes — always once.) A concrete example: a menu that must display before the user makes a choice always uses do-while. A calculation that only runs if data exists uses while.
Nested loop intuition: Before showing the code, ask: “For each of 5 rows, you need to print 5 columns. How many print statements total?” Students calculate 5×5 = 25. Then show that the nested loop does exactly that — the inner loop runs completely for each outer iteration.
Infinite loop deliberately: Have students write a while loop with a deliberate infinite loop (just once, in a controlled environment). Ask: “What was missing?” This cements the importance of the update step.
Methods (B2.3.4)
Motivation:
Ask: “If you need to check whether a number is prime in three different places in your program, what would you do?” Most students say “copy and paste the code.” Then ask: “What if you find a bug in the prime-checking logic after copying it three times — how many places do you need to fix?” The discomfort is the motivation for methods.
Anatomy walkthrough: Write a method signature on the board with four labelled boxes — return type, name, parameter types, parameter names. Map each back to a question:
- Return type: “What comes back from this method?”
- Name: “What does this method do? (use a verb)”
- Parameters: “What does this method need to do its job?”
void vs return: Use a real-world analogy. A printer is void — you give it a document, it prints, nothing comes back. A calculator is not void — you give it numbers, something comes back. Ask students to categorise some methods before writing any code: “Is this a void method or a value-returning method?”
Call stack — brief preview: Each method call pushes a frame onto the call stack. When it returns, the frame is popped. This connects directly to the stack data structure and will be needed again for recursion (HL). A brief mention here plants the seed.
Scope (B2.3.5)
Discover through the error:
Write code where main tries to print a variable declared inside another method. Do not warn students — just show the code and ask “Will this compile?” Then show the compile error: cannot find symbol. Ask: “Why can’t Java find it? Where is it?”
Draw a diagram: each method is a box. Variables declared inside the box exist only inside the box. To pass data between boxes, you use parameters (data goes in) and return values (data comes out). Global (static) variables are declared outside the boxes — they are accessible everywhere in the class.
Guideline for OOP preview: Mention that relying on global variables too heavily is a design problem — you will return to this when covering encapsulation in OOP. Plants the seed without overloading now.
Common Student Mistakes
| Mistake | Description |
|---|---|
Off-by-one in for | i <= n vs i < n confusion — trace carefully for each |
= instead of == in condition | if (x = 5) — compile error in Java (unlike C/C++) but still a common slip |
Missing return | Non-void method compiles but fails if not all paths return a value |
| Infinite loop | Forgetting to update the loop variable (i++ or count--) |
String == in condition | if (name == "Alice") — works sometimes (string literals are interned) but is unreliable |
| Variable declared inside loop | Trying to access a loop variable after the loop ends |
void method with return value | return 5; inside a void method — compile error |
IB Paper 2 Exam Tips
- For loop traces are the most common Paper 2 question type. Students must show every iteration in a table (i, variable values after body). Skipping steps loses marks.
- Know the loop exit condition: “When does the loop stop?” is asked explicitly. The condition becomes false, not “when i reaches n.”
- Method definition vs call: Examiners test whether students know the difference. Definition declares the method; call executes it.
- Scope questions: “Can variable X be accessed at line Y?” — students must reason about which block the variable was declared in.
do-whilefor input validation is a classic exam scenario — menu systems and input loops.- Students often state Big O for loops: a single loop over n elements is O(n); a nested loop is O(n²). This connects to B2.4.