Session 10 Homework: Loops Mission

Fun + Practical + Technical (for a 13-year-old coder)

This worksheet teaches when to use for, while, and do-while using real situations, game-like tasks, and clear coding steps.

First, understand loops like a game developer

A loop means repeating a task until a condition changes. In games and apps, loops are everywhere:

Game frame update

Run character movement, collisions, and score updates repeatedly.

Leaderboard printing

Print all player names one by one from top to bottom.

Login attempts

Keep asking password until correct or attempts end.

Pattern generation

Draw stars, grids, and maps with nested loops.

Which loop should I use?
Loop Use When Real-Life Example Memory Trick
for You know how many times to repeat. Print levels 1 to 10, show table 1 to 10. for = fixed count
while You do not know exact repetitions, depends on condition. Ask password until correct. while = condition first
do-while You must run at least once. Show menu once, then repeat if user wants. do-while = do first, check later
Homework Missions

For each mission, read: Why this loop? then How to think then code and test.

Mission 1: XP Level Counter (for loop)

Print levels from 1 to N

Why for loop? We know exactly how many levels to print.
Thinking steps:
  1. Take input N.
  2. Start i from 1.
  3. Repeat while i <= N.
  4. Print level and increase i.
#include <stdio.h> int main() { int n, i; printf("Enter max level: "); scanf("%d", &n); for (i = 1; i <= n; i++) { printf("Level %d unlocked!\n", i); } return 0; }
  • n = 3 -> Level 1, Level 2, Level 3
  • n = 5 -> prints 5 levels

Mission 2: Sum and Average of Scores (for loop)

Add scores of N rounds and find average

Why for loop? Number of rounds is fixed (N rounds).
Technical concept: This is an accumulator pattern. sum = sum + value on each iteration.
#include <stdio.h> int main() { int n, i, score, sum = 0; float avg; printf("How many rounds? "); scanf("%d", &n); for (i = 1; i <= n; i++) { printf("Enter score for round %d: ", i); scanf("%d", &score); sum += score; } avg = (float)sum / n; printf("Total = %d\n", sum); printf("Average = %.2f\n", avg); return 0; }

Mission 3: Secret PIN System (while loop)

Keep asking until PIN is correct or attempts end

Why while loop? We do not know when user will enter correct PIN.
Important: Update attempt count every time, otherwise infinite loop happens.
#include <stdio.h> int main() { int pin = 4321, entered, attempts = 0; while (attempts < 3) { printf("Enter PIN: "); scanf("%d", &entered); if (entered == pin) { printf("Access granted!\n"); break; } else { attempts++; printf("Wrong PIN. Attempts left: %d\n", 3 - attempts); } } if (attempts == 3) { printf("Account locked.\n"); } return 0; }

Mission 4: Game Menu Loop (do-while)

Show menu at least once, continue until user exits

Why do-while? Menu must appear once before checking continue condition.
#include <stdio.h> int main() { int choice; do { printf("\n=== GAME MENU ===\n"); printf("1. Start Game\n"); printf("2. Settings\n"); printf("3. Exit\n"); printf("Choose: "); scanf("%d", &choice); switch (choice) { case 1: printf("Starting game...\n"); break; case 2: printf("Opening settings...\n"); break; case 3: printf("Goodbye!\n"); break; default: printf("Invalid choice.\n"); } } while (choice != 3); return 0; }

Mission 5: Star Map Builder (nested loops)

Print a rectangle map of stars using rows and columns

Why nested loops? Outer loop handles rows, inner loop handles columns.
2D thinking:
  1. For each row
  2. Print stars for each column
  3. Move to next line
#include <stdio.h> int main() { int rows, cols, r, c; printf("Enter rows and cols: "); scanf("%d %d", &rows, &cols); for (r = 1; r <= rows; r++) { for (c = 1; c <= cols; c++) { printf("* "); } printf("\n"); } return 0; }
  • rows=3, cols=4 -> 3 lines, 4 stars each
  • rows=2, cols=5 -> 2 lines, 5 stars each

Mission 6: Loot Filter (continue + break)

Skip bad items and stop when bag is full

Why break/continue? continue skips unwanted items, break exits early.
#include <stdio.h> int main() { int item, bagCount = 0; for (item = 1; item <= 20; item++) { if (item % 2 == 0) { continue; // skip even-numbered items } printf("Picked item %d\n", item); bagCount++; if (bagCount == 5) { printf("Bag full! Stop looting.\n"); break; } } return 0; }

Boss Challenge (mini project)

Create a Math Practice Game:

- Show menu with do-while (Play / Show Score / Exit)

- In Play mode, ask 5 questions using for loop

- Use while loop to re-ask if user types invalid input

- Use break if user wants to quit early

This one project makes you use all loops practically.