Class 23: For loops, iterators, and the dreaded go-to

Slides

Display version (pdf).

Downloads

Homework

Reading (for next class): PLP, Section 8.4 until the beginning of 8.4.2, and Chapter 13 until the beginning of 13.2.

Exercises

The file Primes.java is supposed to print out all the prime numbers less than 100. It is your starter code for this homework.

This homework is to be submitted electronically, in a folder called class23. You should just submit the single file Primes.java.

  1. Complete the next() and hasNext() methods in the PIter class so that it returns successive prime numbers.
    After this, the program should work and print the primes from 2 to 97.
  2. Now re-write the loop in main as a for-each loop instead of a while loop with an explicit iterator.