Teacher Guide: Selection (if/else)
Student page: Selection IB Syllabus: B2.3.2 Estimated periods: 2 (90 min each) Prerequisites: Variables & Data Types
Contents
Lesson Plans
Period 1: if, if-else, if-else if-else
| Phase | Time | Activity | Student Page Section |
|---|---|---|---|
| Warm-up | 10 min | “A student scored 75. What grade should they get?” — describe decision in English | — |
| Teach | 25 min | if/else, else if chains, boolean operators, condition order | Key Concepts |
| Practice | 40 min | Core #1-4 (Odd/even, Largest of three, Letter grade, Pos/neg/zero) | Practice Exercises |
| Wrap-up | 15 min | Quick Code Check MCQs | Quick Code Check |
Teaching notes:
- Plain English → flowchart → code progression
- Key point: Java evaluates top to bottom, takes FIRST true branch. Demo: check score >= 40 before >= 90, show 95 → wrong grade
- Reinforce
.equals()for String comparison NOW
Additional HW exercises (selection only — no loops):
- Smallest of three, color by number, guess secret number, average/min/max without loops, range validation, withdrawal approval
- “Do not use a loop” constraint reinforces selection practice before loops are introduced
- Include pseudocode alongside Java — connects to computational thinking
Period 2: Nested conditions, boolean logic, switch
| Phase | Time | Activity | Student Page Section |
|---|---|---|---|
| Warm-up | 10 min | “Can you combine two conditions? How?” — introduce && and || | — |
| Teach | 15 min | Nested if, boolean operators, switch-case | Worked Examples |
| Practice | 50 min | Extension #5-7, Challenge #8-9 | Practice Exercises |
| Wrap-up | 15 min | Trace exercise from student page + review | Trace Exercise |
Additional activities:
- Flow of control overview: sequential → selection → iteration → subroutines
- Withdrawal approval exercise: combines
amount > 0,amount <= balance, and confirmation — good compound condition practice
Differentiation
Supporting weaker students:
| Strategy | When to use | Example |
|---|---|---|
| Flowchart first | Can’t translate problem to code | Draw decision tree for letter grade before coding |
| Condition builder worksheet | Can’t write boolean expressions | Fill-in: “Check if age is between 13 and 64” → age >= 13 && age <= 64 |
| Reduce to Core #1-2 | Overwhelmed | Odd/even and largest of three — simplest selection patterns |
| Pair programming | During Core exercises | Stronger student explains condition order |
Extending stronger students:
| Strategy | When to use | Example |
|---|---|---|
| Extra HW exercises | Finishes Core early | Smallest of three, guess secret number, withdrawal approval |
| Challenge exercises | Extended learners | #8 Triangle classifier, #9 Leap year |
| Short-circuit evaluation | After boolean operators | “Does Java evaluate the second condition if the first is false in &&?” |
| Pseudocode translation | After Challenge | Write pseudocode for triangle classifier, then implement |
Answer Keys
Core 1: Odd or even
import java.util.Scanner;
public class OddOrEven {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num % 2 == 0) {
System.out.println(num + " is even");
} else {
System.out.println(num + " is odd");
}
scanner.close();
}
}
Expected output: 7 → 7 is odd, 4 → 4 is even
Common mistakes: Using num / 2 == 0 instead of num % 2 == 0
Marking notes: Accept num % 2 != 0 for odd check
Core 2: Largest of three
import java.util.Scanner;
public class LargestOfThree {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter three numbers: ");
int a = scanner.nextInt();
int b = scanner.nextInt();
int c = scanner.nextInt();
int largest;
if (a >= b && a >= c) {
largest = a;
} else if (b >= c) {
largest = b;
} else {
largest = c;
}
System.out.println("Largest: " + largest);
scanner.close();
}
}
Common mistakes: Not handling equal values; using nested if instead of && compound condition
Marking notes: Accept Math.max approach if students discover it, but not required
Core 3: Letter grade
import java.util.Scanner;
public class LetterGrade {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter score (0-100): ");
int score = scanner.nextInt();
int grade;
if (score >= 90) {
grade = 7;
} else if (score >= 80) {
grade = 6;
} else if (score >= 70) {
grade = 5;
} else if (score >= 60) {
grade = 4;
} else if (score >= 50) {
grade = 3;
} else if (score >= 40) {
grade = 2;
} else {
grade = 1;
}
System.out.println("IB Grade: " + grade);
scanner.close();
}
}
Common mistakes: Wrong condition order (checking >= 40 first catches everything); using == instead of >=
Marking notes: Grade boundaries may vary — accept any reasonable set. Key skill is correct ordering of else-if chain.
Core 4: Positive, negative, or zero
import java.util.Scanner;
public class PosNegZero {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
if (num > 0) {
System.out.println("Positive");
} else if (num < 0) {
System.out.println("Negative");
} else {
System.out.println("Zero");
}
scanner.close();
}
}
Common mistakes: Using num == 0 first and missing the else-if pattern
Extension 5: Ticket pricing
import java.util.Scanner;
public class TicketPricing {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter age: ");
int age = scanner.nextInt();
if (age < 0) {
System.out.println("Invalid age");
} else if (age < 5) {
System.out.println("Free");
} else if (age <= 12) {
System.out.println("$8");
} else if (age <= 64) {
System.out.println("$15");
} else {
System.out.println("$10");
}
scanner.close();
}
}
Common mistakes: Missing the negative age validation; using > instead of >= for boundary ages
Marking notes: Must validate negative age. Accept any reasonable output format for prices.
Extension 6: Quadrant finder
import java.util.Scanner;
public class QuadrantFinder {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter x: ");
int x = scanner.nextInt();
System.out.print("Enter y: ");
int y = scanner.nextInt();
if (x == 0 || y == 0) {
System.out.println("On an axis");
} else if (x > 0 && y > 0) {
System.out.println("Quadrant I");
} else if (x < 0 && y > 0) {
System.out.println("Quadrant II");
} else if (x < 0 && y < 0) {
System.out.println("Quadrant III");
} else {
System.out.println("Quadrant IV");
}
scanner.close();
}
}
Common mistakes: Not checking for axis case (x==0 or y==0) — must check this first; confusing quadrant numbering
Extension 7: Day of the week
import java.util.Scanner;
public class DayOfWeek {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter day number (1-7): ");
int day = scanner.nextInt();
String name;
if (day == 1) {
name = "Monday";
} else if (day == 2) {
name = "Tuesday";
} else if (day == 3) {
name = "Wednesday";
} else if (day == 4) {
name = "Thursday";
} else if (day == 5) {
name = "Friday";
} else if (day == 6) {
name = "Saturday";
} else if (day == 7) {
name = "Sunday";
} else {
name = "Invalid";
}
System.out.println(name);
scanner.close();
}
}
Common mistakes: Forgetting the “Invalid” case for numbers outside 1-7
Marking notes: Accept switch-case as alternative; starting from Sunday or Monday both acceptable
Challenge 8: Triangle classifier
import java.util.Scanner;
public class TriangleClassifier {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter side a: ");
int a = scanner.nextInt();
System.out.print("Enter side b: ");
int b = scanner.nextInt();
System.out.print("Enter side c: ");
int c = scanner.nextInt();
// Triangle inequality: sum of any two sides must be greater than the third
if (a + b > c && a + c > b && b + c > a) {
if (a == b && b == c) {
System.out.println("Equilateral");
} else if (a == b || a == c || b == c) {
System.out.println("Isosceles");
} else {
System.out.println("Scalene");
}
} else {
System.out.println("Not a valid triangle");
}
scanner.close();
}
}
Common mistakes: Forgetting triangle inequality check; not checking all three pairs for isosceles
Marking notes: Must validate triangle inequality BEFORE classifying. All three inequality conditions required.
Challenge 9: Leap year checker
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeap;
if (year % 400 == 0) {
isLeap = true;
} else if (year % 100 == 0) {
isLeap = false;
} else if (year % 4 == 0) {
isLeap = true;
} else {
isLeap = false;
}
if (isLeap) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
scanner.close();
}
}
Common mistakes: Wrong order of checks (must check % 400 before % 100); not understanding the century exception
Marking notes: Order matters — % 400 → % 100 → % 4. Accept one-line boolean: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0). Key test cases: 2000 (leap), 1900 (not leap), 2024 (leap), 2023 (not leap).
Integration Notes
| Item | In Class | Homework | Link |
|---|---|---|---|
| Worked Examples | Walk through together | — | Worked Examples |
| Quick Code Check | End of Period 1 | — | Quick Code Check |
| Core #1-4 | Period 1 practice | — | Practice Exercises |
| Extension #5-7 | Start in Period 2 | Complete for homework | Practice Exercises |
| Challenge #8-9 | — | Optional extension | Practice Exercises |
| Trace Exercise | Period 2 wrap-up | — | Trace Exercise |
| GitHub Classroom | — | Due end of week | GitHub Classroom |
Additional Resources
- Extra HW exercises (selection only, no loops): smallest of three, color by number, guess secret number, avg/min/max without loops, range validation, withdrawal approval
- Flowchart templates (blank decision diamonds for students to fill in)
- Pseudocode ↔ Java translation worksheet