// Illustrate while, for, and do loops. // Also illustrate the switch statement. public class Structured { public static void main(String[] args) { int x; String word; // Using a while loop x = 1; while(x < 100) { System.out.print(x + " "); x *= 2; } System.out.println(); // Using a for loop for(x = 1; x < 100; x *= 2) System.out.print(x + " "); System.out.println(); // Using a do loop. A do loop is guarenteed // to execute at least once. x = 1; do { System.out.print(x + " "); x *= 2; } while(x < 100); System.out.println(); System.out.println(); // Using a switch statement. A switch // statement chooses from among a list // of constants. x = 2; switch(x) { case 1: word = "one"; break; case 2: word = "two"; break; case 3: word = "three"; break; default: word = "many"; } System.out.println("word == \"" + word + "\""); // Exercise. Convert the preceding // switch statement to an if..else // statement. } } // Output: 1 2 4 8 16 32 64 1 2 4 8 16 32 64 1 2 4 8 16 32 64 word == "two"