Loop Basics

  • Read this code. (Remember to run the code for yourself, see what it does, and try experimenting with it. Here are the instructions.)
  • for-each loop
  • while loop
  • C-style for loop
  • break, continue
  • infinite loop
  • off-by-one error
  1. What is the purpose of each of the three expressions that appear in the control line of a C-style for loop?
  2. The reading mentions three common looping patters that we use in programming:

    Pattern name Java idiom Java syntax
    repeat until sentinel while loop while(___) { ... }
    do n times C-style for loop for(___; ___; ___) { ... }
    for each element for-each loop for(___ : ___) { ... }

    In what situations would you want to use each one of these?

What was particularly interesting, confusing, or exciting to you in this reading?