Module 2: Decisions and loops

Make programs choose (if/elif/else) and repeat (while, for, range), from grade calculators to star patterns and number programs.

  1. if / elif / else: == compares and = stores; an elif ladder runs only the first True branch, so put the strictest condition first.
  2. Combining conditions: and needs both, or needs one, not flips; 0, "" and None are falsy; write ch == "a" or ch == "e", never ch == "a" or "e".
  3. Loops: while and for: while repeats while a condition is True (do not forget the update); range(a, b, s) goes from a up to but not including b.
  4. Loop patterns: Start the result before the loop, update it inside, use it after; continue skips a round, break leaves, loop else runs only if there was no break.
  5. Nested loops and patterns: The outer loop counts rows, the inner loop fills a row; end each row with print(), and work out spaces and stars per row with a small table.
  6. Number programs: n % 10 is the last digit and n // 10 drops it; a product starts at 1, a prime check needs a flag or a loop else, and you should always test 0, 1 and 2.

Ends with a 13-question checkpoint (80% passes) and a mini project: Canteen Fest: guess the number.