/* ForExamples.java example code AC Chapin */ package aaa; public class ForExamples { public static void main(String[] args) { // counting with a while loop int i = 0; while (i < 100) { System.out.println(i); i++; } // equivalent for loop for (int j = 0; j < 100; j++) { System.out.println(j); } int count; //counter int initial = 0; // counter's initial value int bound = 10; // counter's upper bound int increment = 2; // how to change counter for (count = initial; // set up counter count < bound; // check counter count += increment) { // update counter // code to repeat // bad idea to change // bound, increment, or counter here } // equivalent for (int x = 0; x < 10; x += 2) { // whatever } // counting down by 5's from 5000 to 500 for (int k = 5000; k >= 500; k = k - 5) { System.out.println(k); } // nested for loops for (i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { for (int k = 0; k < 3; k++) { System.out.println("(" + i + ", " + j + ", " + k + ")"); } } } // print numbers from 1 to 9 in a triangle // the outer loop tells how many rows to do for (i = 1; i < 10; i++) { // the inner loop goes across the row // counting up to i each time // so as i gets bigger, the row gets longer for (int j = 1; j <= i; j++) { // technically, j only exists within the curly braces // of the previous for loop it was in, so we can declare // j again for this for loop System.out.print(j + " "); } System.out.println(); } /* // PROBLEMS WITH LOOPS // x moving in wrong direction for (int x = 0; x < 10; x--) { //... } // using counter for input for (int x = 0; x < 10; x++) { System.out.println("Number please?") x = scan.nextInt(); } // moveing the goalposts for (int x = 0; x < y; x++) { y++; } */ } }