Loop Basics

  1. 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: “repeat until sentinel,” which uses a while loop; “do n times,” which uses a for loop in Java; and “for each element,” which also uses a for loop in Java. In what situations would you want to use each one of these?

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