CS101 · Annotated SlidesChapter 5 — Loops
Chapter 5 · Annotated slides

Loops

while, do-while and for loops, sentinel values, which loop to use, nested loops, numerical errors, break and continue, and the classic loop problems: guessing numbers, GCD, future tuition, palindromes and primes.

Slide text Study notes Highlight: key words Tips, tricks & traps Seen again elsewhere How it can come up

Loops

CS101 · Chapter 5
1
Tipwhile: test first, may run 0 times · do-while: test after, runs at least once · for: counter loops (init; condition; update).
Slide 2: Motivations
Exam weight Low
  1. Loops repeat statements instead of copying them 100 times.
Slide 3: Opening Problem
Exam weight Low
  1. Opening problem: print a string 100 times.
Slide 4: Introducing while Loops
Exam weight Low
  1. while (count < 100) { …; count++; }
Slide 5: while § loop (§5.2). §
Exam weight Low
  1. while loop: checks the condition before each iteration.
Slide 6: while
Exam weight High
  1. while flowchart.
🎯 How this can come up · 1 question
MCQ

How many times does int i = 0; while (i < 5) i++; run the body?

Slide 7: animation
Exam weight Low
  1. Trace.
Slide 8: animation
Exam weight Low
  1. Trace.
Slide 9: animation
Exam weight Low
  1. Trace.
Slide 10: animation
Exam weight Low
  1. Trace.
Slide 11: animation
Exam weight Low
  1. Trace.
Slide 12: animation
Exam weight Low
  1. Trace.
Slide 13: animation
Exam weight Low
  1. Trace.
Slide 14: animation
Exam weight Low
  1. Trace.
Slide 15: animation
Exam weight Low
  1. Trace.
Slide 16: Problem: Repeat Addition Until Correct
Exam weight Low
  1. Repeat a question until the answer is correct.
Slide 17: Ending a Loop with a Sentinel Value
Exam weight Low
  1. Sentinel value: a special input (e.g., 0) that ends the loop.
Slide 18: Problem: An Advanced Math Learning Tool
Exam weight Low
  1. Advanced math tool: ten questions, count correct answers.
Slide 19: Problem: Guessing Numbers
Exam weight Low
  1. Guessing numbers: higher/lower until correct.
Slide 20: Caution
Exam weight Medium
  1. Caution: don’t compare floating-point values for equality in loop conditions.
Exam trapwhile (item != 0) with doubles may never stop because of round-off.
Slide 21: do-while
Exam weight High
  1. do-while: body first, then test: runs at least once.
🎯 How this can come up · 1 question
MCQ

Which loop always executes its body at least once?

Slide 22: /*(Find the character of an ASCII code) Write a program that receives an ASCII code(an integer between 0 and 127) and displays its character.*/ Scanner input = new Scanner(System.in); // Prompt the user to enter an ASCII code
Exam weight Low
  1. Exercise: read an ASCII code and print its character.
Slide 23: for
Exam weight High
  1. for (initial-action; continuation-condition; action-after-each-iteration).
🎯 How this can come up · 1 question
MCQ

How many lines does for (int i = 1; i <= 10; i += 3) print?

Slide 24: animation
Exam weight Low
  1. Trace.
Slide 25: animation
Exam weight Low
  1. Trace.
Slide 26: animation
Exam weight Low
  1. Trace.
Slide 27: animation
Exam weight Low
  1. Trace.
Slide 28: animation
Exam weight Low
  1. Trace.
Slide 29: animation
Exam weight Low
  1. Trace.
Slide 30: animation
Exam weight Low
  1. Trace.
Slide 31: animation
Exam weight Low
  1. Trace.
Slide 32: animation
Exam weight Low
  1. Trace.
Slide 33: animation
Exam weight Low
  1. Trace.
Slide 34: Note
Exam weight Low
  1. The initial and update parts may hold several comma-separated statements.
Slide 35: Note
Exam weight Low
  1. If the condition is omitted it is implicitly true: for ( ; ; ) is infinite.
Slide 36: Caution
Exam weight High
  1. Caution: a semicolon right after for (...) ; makes an empty body.
🎯 How this can come up · 1 question
Written

What is wrong with: for (int i = 0; i < 10; i++); { System.out.println("i is " + i); }

Slide 37: Caution, cont.
Exam weight Low
  1. Same caution for while.
Slide 38: Which Loop to Use?
Exam weight Medium
  1. The three loops are equivalent; use for when the count is known, while for unknown counts, do-while when the body must run first.
Slide 39: Recommendations
Exam weight Low
  1. Recommendations on choosing loops.
Slide 40: Nested Loops
Exam weight High
  1. Nested loops: the inner loop runs fully for each outer iteration (multiplication table).
🎯 How this can come up · 1 question
MCQ

for (i = 0; i < 3; i++) for (j = 0; j < 4; j++) print. How many prints?

Slide 41: Minimizing Numerical Errors
Exam weight Low
  1. Minimize numerical errors: accumulate small numbers first, use integer counters.
Slide 42: Problem: Finding the Greatest Common Divisor
Exam weight Low
  1. GCD by testing k from 2 up while k ≤ n1 and k ≤ n2.
Slide 43: Problem: Predicting the Future Tuition
Exam weight Low
  1. Future tuition: years until tuition doubles at 7% a year.
Slide 44: break continue
Exam weight Low
  1. break exits the loop; continue skips to the next iteration.
Slide 45: break
Exam weight High
  1. break example: stop when sum > 100.
🎯 How this can come up · 1 question
MCQ

Which statement immediately ends the innermost loop?

Slide 46: continue
Exam weight High
  1. continue example: skip 10 and 11 in the sum.
🎯 How this can come up · 1 question
MCQ

continue in a loop:

Slide 47: Guessing Number Problem Revisited
Exam weight Low
  1. Guessing number with break.
Slide 48: Problem: Checking Palindrome
Exam weight Low
  1. Palindrome: compare characters from both ends moving inward.
Slide 49: Write a program to get a number from user and test whether it is prime or not. An integer greater than 1 is prime if its only positive divisor is 1 or itself. For example, 2, 3, 5, and 7 are prime numbers, but 4, 6, 8, and 9 are not
Exam weight High
  1. Prime test: divide by 2 … sqrt(n); prime if no divisor found.
🎯 How this can come up · 1 question
Written

Write a loop that checks whether n > 1 is prime.

Slide 50: Problem: Displaying Prime Numbers
Exam weight Low
  1. Display the first 50 primes, 10 per line.