Skip to content
Ameen Abozaid
BEG-013

Loops

  • 7 Lessons
  • 70 min of reading
  • 5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • for
  • enhanced for
  • while
  • do-while
  • break
  • continue
  • nested loops

Learn first

70 min of reading · 5 h of practice (estimate)

Learning outcomes

  1. Choose for, while, or do-while for a given task and justify the choice.
  2. Write loops with correct start, condition, and update, avoiding off-by-one and infinite loops.
  3. Use break and continue appropriately.
  4. Write nested loops for grid-style problems and estimate how many times the body runs.

Lessons

  1. 01Why loops: repetition without copy-paste10 min read
  2. 02The for loop10 min read
  3. 03while and do-while10 min read
  4. 04break, continue, and loop control10 min read
  5. 05Nested loops10 min read
  6. 06Off-by-one errors and infinite loops10 min read
  7. 07Demo: number guessing game, stepping through the loop in the debugger10 min read

Lab and exercises

Lab

BEG-013-LAB1 · Number guessing game (mini-project 1 of 3)

Outcomes: 1–4 · Time: ~3 h

  1. Pick a random target between 1 and 100.
  2. Allow a limited number of attempts (for example 7); give higher/lower hints.
  3. Reject input that is not a number or is out of range without losing an attempt.
  4. Report the number of attempts used at the end.
  5. Ask "play again?" and replay without restarting the program.
  6. Step through one game in the debugger.

Acceptance: handles invalid input, reports attempts, replay works.

Exercises (assessment: seven exercises plus the guessing game)

IDOutcomeTask
BEG-013-EX011For each task pick for, while, or do-while and justify: print 1-10; read until "quit"; a menu shown at least once.
BEG-013-EX022Print the sum of 1 to 100 with a for loop.
BEG-013-EX032This loop never ends. Fix it: int i = 0; while (i < 10) { System.out.println(i); }
BEG-013-EX042Print the numbers 10 down to 1 with no off-by-one error.
BEG-013-EX053Print 1-20, skipping multiples of 3 and stopping at the first number above 15.
BEG-013-EX064Print a 5-row triangle of stars.
BEG-013-EX074Print the multiplication table 1-9 and state how many times the body runs.
Show solutions
  • EX01 for (known count); while (unknown count, may run zero times); do-while (at least once).
  • EX02 int sum = 0; for (int i = 1; i <= 100; i++) sum += i; result 5050.
  • EX03 Add i++; inside the body.
  • EX04 for (int i = 10; i >= 1; i--).
  • EX05 for (int i = 1; i <= 20; i++) { if (i > 15) break; if (i % 3 == 0) continue; System.out.println(i); }
  • EX06 for (int r = 1; r <= 5; r++) { for (int c = 1; c <= r; c++) System.out.print("*"); System.out.println(); }
  • EX07 Nested loops 1..9 by 1..9; the body runs 81 times.
  • The guessing game and quiz also cover outcomes 2 and 3 further.

Quiz

Outcomes: O1 choosing a loop · O2 correct loop parts · O3 break/continue · O4 nested loops.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Best loop when you know the exact count:O1
  2. 2Which loop always runs its body at least once?O1
  3. 3How many times does for (int i = 0; i < 5; i++) run?O2
  4. 4while (x < 10) { print(x); } with no change to x is:O2
  5. 5An off-by-one error often comes from mixing up:O2
  6. 6break inside a loop:O3
  7. 7continue inside a loop:O3
  8. 8In nested loops, break leaves:O3
  9. 9Outer loop 4 times, inner loop 5 times: the inner body runsO4
  10. 10To print a grid of rows and columns you use:O4

Practical part

Seven exercises BEG-013-EX01 to EX07 plus the guessing game. Pass: at least six exercises correct and a working guessing game that meets every acceptance point.

Common mistakes

Off-by-one boundaries; modifying the loop variable in the body; forgetting to update the while condition.

Assessment

7 exercises plus the guessing game.

Career relevance

Iteration cost leads into complexity (BEG-039).

Type to search levels, phases, modules and lessons.