Exercises

  1. 4.5 Describe the four basic elements of counter-controlled iteration.

  2. 4.6 Compare and contrast the while and for iteration statements.

  3. 4.7 Discuss a situation in which it would be more appropriate to use a do…while statement than a while statement. Explain why.

  4. 4.8 Compare and contrast the break and continue statements.

  5. 4.9 Find and correct the error(s) in each of the following segments of code:

    1. 
      1   For (i = 100, i >= 1, i++) {
      2      System.out.println(i);
      3   }
      
    2. The following code should print whether integer value is odd or even:

      
      1   switch (value % 2) {
      2      case 0:
      3         System.out.println("Even integer");
      4      case 1:
      5         System.out.println("Odd integer");
      6   }
      
    3. The following code should output the odd integers from 19 to 1:

      
      1   for (i = 19; i >= ...

Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.