FivePerLine.java


Below is the syntax highlighted version of FivePerLine.java from §1.3 Conditionals and Loops.


/*************************************************************************
 *  Compilation:  javac FivePerLine.java
 *  Execution:    java FivePerLine
 *  
 *  Print the integers from 1000 to 2000, 5 per line.
 *
 *  % java FivePerLine
 *
 *************************************************************************/

public class FivePerLine { 

   public static void main(String[] args) {

       // print integers from 1000 to 2000, 5 per line
       int start = 1000, end = 2000;
       for (int i = start; i <= end; i++) {
          System.out.print(i + " ");
          if (i % 5 == 4) System.out.println();
       }
       System.out.println();

   }
}


Copyright © 2007, Robert Sedgewick and Kevin Wayne.
Last updated: Tue Sep 29 16:17:41 EDT 2009.