Home  Listings  By Name  By Subject  Email  www.tomswan.com  Help 

Java 2 Just Click! Solutions

Chapter 5 Listings

© 2001 by Tom Swan. All rights reserved. Updated: 6/4/01 12:20:54 PM

Return to Listings page
Switch to Solutions by name page
Switch to Solutions by subject page


Listing 5-1 Switcher/Switcher.java Page 72

Return to top
001: class Switcher {
002:  public static void main(String args[]) {
003:   int a = 2;
004:   switch (a) {
005:    case 1:
006:     System.out.println("Case 1");
007:     break;
008:    case 2:
009:     System.out.println("Case 2");
010:     System.out.println("Final statement in case 2");
011:     break;
012:    case 3:
013:     System.out.println("Case 3");
014:     break;
015:    default:
016:     System.out.println("All other cases");
017:   }
018:  }
019: }
Return to top

Listing 5-2 WhileCount/WhileCount.java Page 74

Return to top
001: class WhileCount {
002:  public static void main(String args[]) {
003:   int count = 0;
004:   while (count < 10) {
005:    count++;
006:    System.out.println("Count = " + count);
007:   }
008:  }
009: }
Return to top

Listing 5-3 DoWhileCount/DoWhileCount.java Page 74

Return to top
001: class DoWhileCount {
002:  public static void main(String args[]) {
003:   int count = 0;
004:   do {
005:    count++;
006:    System.out.println("Count = " + count);
007:   } while (count < 10);
008:  }
009: }
Return to top

Listing 5-4 ForCount/ForCount.java Page 75

Return to top
001: class ForCount {
002:  public static void main(String args[]) {
003:   int count;
004:   for (count = 1; count <= 10; count++) {
005:    System.out.println("Count = " + count);
006:   }
007:  }
008: }
Return to top

Listing 5-5 LabelDemo/LabelDemo.java Page 76

Return to top
001: class LabelDemo  {
002:  public static void main(String args[]) {
003:   int i, j;
004: OuterLoop:
005:   for (i = 1; i < 100; i++) {
006:    System.out.println("/nOuter loop # " + i);
007: InnerLoop:
008:    for (j = 1; j < 10; j++) {
009:     if (j % 2 == 0)
010:      continue InnerLoop;  // Skip even j values
011:     if (i > 4)
012:      break OuterLoop;     // Abort if i > 4
013:     System.out.println("j = " + j);
014:    }  // end of inner for statement
015:   }   // end of outer for statement
016:    System.out.println("Program exiting at OuterLoop:");
017:  }    // end of main() method
018: }     // end of class declaration
Return to top