/* * Code Snippets for Chapt 10 */ public class Chapt10Snippets { public static void main(String[] args) { test0( ); test1( ); test2( ); test4( ); } public static void test0( ) { int option = 9; if ( option == 1 ) System.out.println( "Read image" ); else if (option == 2 ) System.out.println( "Double" ); else if ( option == 9 ) System.out.println( "Quit" ); else System.out.println( "Sorry, invalid option" ); switch ( option ) { case 1: System.out.println( "Read image" ); break; case 2: System.out.println( "Double" ); break; case 9: System.out.println( "Quit" ); break; default: System.out.println( "Sorry, invalid option" ); break; } /* int */ option = 2; switch ( option ) { case 1: System.out.println( "Read image" ); case 2: System.out.println( "Double" ); case 9: System.out.println( "Quit" ); default: System.out.println( "Sorry, invalid" ); } char command = 'D'; switch ( command ) { case 'i': case 'I': System.out.println( "Insert" ); break; case 'd': case 'D': System.out.println( "Delete" ); break; case 'r': case 'R': System.out.println( "Replace" ); break; } } public static void test1( ) { int option = 1; switch ( option ) { case 2: System.out.println( "Case 2" ); break; case 1: System.out.println( "Case 1" ); break; default: System.out.println( "Default case" ); break; case 9: System.out.println( "Case 9" ); break; } } public static void test2( ) { double sum; int n; sum = 0.0; n = 1; while ( n <= 5 ) { sum += n; n++; } System.out.println( "sum: " + sum + " n: " + n ); sum = 0.0; for ( n = 1; n <= 5 ; n++ ) { sum += n; } System.out.println( "sum: " + sum + " n: " + n ); int count; int m; int max = 4; for ( count = 4; count >= 0; count-- ) System.out.println( count + " bottles of beer on the wall" ); // Loop that counts by twos, from 0, 2, 4, ..., 2*max: for ( m = 0; m <= 2*max; m += 2 ) { // ... something exciting ... System.out.println( "m: " + m ); } // The loop-control variable can be defined within the initialization. // Note that the scope of the variable is limited to the for-loop: for ( int i = 0; i < 20; i++ ) { sum = sum + i; } // Does not compile: System.out.println ( i ); // Does not compile: for ( int j = 0; j < 100; j++; ) System.out.println( j ); int k; for ( k = 0; k < 10; k++ ) ; System.out.println( k ); int z; int low = 4; int high = 6; for ( z = low; z != high; z ++ ) { // loops infinitely if high < low System.out.println( "z = " + z ); } } static void test3( ) { int[] sequence = { 3, 6, 13, 19, 25, 22, 28, 32, -999 }; boolean isIncreasing = true; int i = 0; int prev = sequence[i++]; int curr = sequence[i++]; while ( curr != -999 ) { if ( prev >= curr ) { isIncreasing = false; break; } prev = curr; curr = sequence[i++]; } if ( isIncreasing ) System.out.println( "Increasing" ); else System.out.println( "Non-increasing" ); } static void test4( ) { int count; for (count = 1; count <= 500; count++ ) { if ( Math.random( ) < 0.01 ) break; } System.out.println( "count = " + count ); boolean foundIt = false; for (count = 1; ( count <= 500 && ! foundIt) ; count++ ) { if ( Math.random( ) < 0.01 ) { foundIt = true; count--; } } System.out.println( "count = " + count ); } }