CS101 · Chapter 5 · Annotated SlidesLoops
Original
Overview

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.

How to read this page

Every original slide is shown unchanged. Yellow marks on a slide point to a numbered note: hover or tap a mark to jump to its note, and hover a note to see its marks. Questions tagged Past exam appeared on a real paper; Prediction questions are likely angles, not confirmed. Pick an answer to each question: a wrong pick tells you why it is wrong, so you can try again.

All 50 slides
  1. 1 Loops
  2. 2 Motivations
  3. 3 Opening Problem
  4. 4 Introducing while Loops
  5. 5 while § loop (§5.2). §
  6. 6 while
  7. 7 animation
  8. 8 animation
  9. 9 animation
  10. 10 animation
  11. 11 animation
  12. 12 animation
  13. 13 animation
  14. 14 animation
  15. 15 animation
  16. 16 Problem: Repeat Addition Until Correct
  17. 17 Ending a Loop with a Sentinel Value
  18. 18 Problem: An Advanced Math Learning Tool
  19. 19 Problem: Guessing Numbers
  20. 20 Caution
  21. 21 do-while
  22. 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
  23. 23 for
  24. 24 animation
  25. 25 animation
  26. 26 animation
  27. 27 animation
  28. 28 animation
  29. 29 animation
  30. 30 animation
  31. 31 animation
  32. 32 animation
  33. 33 animation
  34. 34 Note
  35. 35 Note
  36. 36 Caution
  37. 37 Caution, cont.
  38. 38 Which Loop to Use?
  39. 39 Recommendations
  40. 40 Nested Loops
  41. 41 Minimizing Numerical Errors
  42. 42 Problem: Finding the Greatest Common Divisor
  43. 43 Problem: Predicting the Future Tuition
  44. 44 break continue
  45. 45 break
  46. 46 continue
  47. 47 Guessing Number Problem Revisited
  48. 48 Problem: Checking Palindrome
  49. 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
  50. 50 Problem: Displaying Prime Numbers
Slide 1 / 50

Loops

Slide 1: Loops

Notes

  1. Tip while: test first, may run 0 times · do-while: test after, runs at least once · for: counter loops (init; condition; update).

Slide 2 / 50

Motivations

Slide 2: Motivations

Notes

  1. Loops repeat statements instead of copying them 100 times.

Slide 3 / 50

Opening Problem

Slide 3: Opening Problem
Slide 4 / 50

Introducing while Loops

Slide 4: Introducing while Loops
Slide 5 / 50

while § loop (§5.2). §

Slide 5: while § loop (§5.2). §

Notes

  1. while loop: checks the condition before each iteration.

Slide 6 / 50

while

Slide 6: while

Notes

  1. while flowchart.

Exam angle

PredictionQ1

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

Slide 7 / 50

animation

Slide 7: animation

Notes

  1. Trace.

Slide 8 / 50

animation

Slide 8: animation

Notes

  1. Trace.

Slide 9 / 50

animation

Slide 9: animation

Notes

  1. Trace.

Slide 10 / 50

animation

Slide 10: animation

Notes

  1. Trace.

Slide 11 / 50

animation

Slide 11: animation

Notes

  1. Trace.

Slide 12 / 50

animation

Slide 12: animation

Notes

  1. Trace.

Slide 13 / 50

animation

Slide 13: animation

Notes

  1. Trace.

Slide 14 / 50

animation

Slide 14: animation

Notes

  1. Trace.

Slide 15 / 50

animation

Slide 15: animation

Notes

  1. Trace.

Slide 16 / 50

Problem: Repeat Addition Until Correct

Slide 16: Problem: Repeat Addition Until Correct
Slide 17 / 50

Ending a Loop with a Sentinel Value

Slide 17: Ending a Loop with a Sentinel Value
Slide 18 / 50

Problem: An Advanced Math Learning Tool

Slide 18: Problem: An Advanced Math Learning Tool
Slide 19 / 50

Problem: Guessing Numbers

Slide 19: Problem: Guessing Numbers

Notes

  1. Guessing numbers: higher/lower until correct.

Slide 20 / 50

Caution

Slide 20: Caution

Notes

  1. Trap while (item != 0) with doubles may never stop because of round-off.

Slide 21 / 50

do-while

Slide 21: do-while

Notes

  1. do-while: body first, then test: runs at least once.

Exam angle

PredictionQ1

Which loop always executes its body at least once?

Slide 22 / 50

/*(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

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
Slide 23 / 50

for

Slide 23: for

Exam angle

PredictionQ1

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

Slide 24 / 50

animation

Slide 24: animation

Notes

  1. Trace.

Slide 25 / 50

animation

Slide 25: animation

Notes

  1. Trace.

Slide 26 / 50

animation

Slide 26: animation

Notes

  1. Trace.

Slide 27 / 50

animation

Slide 27: animation

Notes

  1. Trace.

Slide 28 / 50

animation

Slide 28: animation

Notes

  1. Trace.

Slide 29 / 50

animation

Slide 29: animation

Notes

  1. Trace.

Slide 30 / 50

animation

Slide 30: animation

Notes

  1. Trace.

Slide 31 / 50

animation

Slide 31: animation

Notes

  1. Trace.

Slide 32 / 50

animation

Slide 32: animation

Notes

  1. Trace.

Slide 33 / 50

animation

Slide 33: animation

Notes

  1. Trace.

Slide 34 / 50

Note

Slide 34: Note

Notes

  1. The initial and update parts may hold several comma-separated statements.

Slide 35 / 50

Note

Slide 35: Note
Slide 36 / 50

Caution

Slide 36: Caution

Notes

  1. Caution: a semicolon right after for (...) ; makes an empty body.

Exam angle

PredictionQ1

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

Compare with the model answer

The semicolon gives the loop an empty body; the block runs once afterwards (and i is out of scope there, so it does not even compile).

Slide 37 / 50

Caution, cont.

Slide 37: Caution, cont.
Slide 38 / 50

Which Loop to Use?

Slide 38: Which Loop to Use?

Notes

  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 / 50

Recommendations

Slide 39: Recommendations

Notes

  1. Recommendations on choosing loops.

Slide 40 / 50

Nested Loops

Slide 40: Nested Loops

Notes

  1. Nested loops: the inner loop runs fully for each outer iteration (multiplication table).

Exam angle

PredictionQ1

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

Slide 41 / 50

Minimizing Numerical Errors

Slide 41: Minimizing Numerical Errors

Notes

  1. Minimize numerical errors: accumulate small numbers first, use integer counters.

Slide 42 / 50

Problem: Finding the Greatest Common Divisor

Slide 42: Problem: Finding the Greatest Common Divisor

Notes

  1. GCD by testing k from 2 up while k ≤ n1 and k ≤ n2.

Slide 43 / 50

Problem: Predicting the Future Tuition

Slide 43: Problem: Predicting the Future Tuition
Slide 44 / 50

break continue

Slide 44: break continue

Notes

  1. break exits the loop; continue skips to the next iteration.

Slide 45 / 50

break

Slide 45: break

Exam angle

PredictionQ1

Which statement immediately ends the innermost loop?

Slide 46 / 50

continue

Slide 46: continue

Notes

  1. continue example: skip 10 and 11 in the sum.

Exam angle

PredictionQ1

continue in a loop:

Slide 47 / 50

Guessing Number Problem Revisited

Slide 47: Guessing Number Problem Revisited
Slide 48 / 50

Problem: Checking Palindrome

Slide 48: Problem: Checking Palindrome

Notes

  1. Palindrome: compare characters from both ends moving inward.

Slide 49 / 50

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

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

Notes

  1. Prime test: divide by 2 … sqrt(n); prime if no divisor found.

Exam angle

PredictionQ1

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

Compare with the model answer

boolean isPrime = true; for (int d = 2; d <= Math.sqrt(n); d++) { if (n % d == 0) { isPrime = false; break; } }

Slide 50 / 50

Problem: Displaying Prime Numbers

Slide 50: Problem: Displaying Prime Numbers