|
BreakAndContinueDemo |
|
1 // Example 3.3 joi/examples/BreakAndContinueDemo.java 2 // 3 // 4 // Copyright 2003 Bill Campbell and Ethan Bolker 5 6 public class BreakAndContinueDemo 7 { 8 private static Terminal t = new Terminal(); 9 10 public static void main( String[] args ) 11 { 12 t.println("invoking loop"); 13 BreakAndContinueDemo.loop(); // could say just loop(); 14 t.println("returned from loop, leaving main"); 15 } 16 17 private static void loop() 18 { 19 t.println("starting infinite loop"); 20 while( true ) { 21 String command = t.readWord( 22 "normal, break, continue, return, exit, oops ? > "); 23 if (command.startsWith("n")) { 24 t.println("normal flow of control"); 25 } 26 if (command.startsWith("b")) { 27 t.println("break from looping"); 28 break; 29 } 30 if (command.startsWith("c")) { 31 t.println("continue looping"); 32 continue; 33 } 34 if (command.startsWith("r")) { 35 t.println("return prematurely from loop method"); 36 return; 37 } 38 if (command.startsWith("e")) { 39 t.println("exit prematurely from program"); 40 System.exit(0); 41 } 42 if (command.startsWith("o")) { 43 t.println("program about to crash ..."); 44 Terminal foo = null; 45 foo.println("crash the program"); 46 } 47 t.println("last line in loop body"); 48 } 49 t.println("first line after loop body"); 50 t.println("returning normally from loop method"); 51 } 52 } 53
|
BreakAndContinueDemo |
|