Lab Fri 2/15 - Sample solution Problem a, b, and c are from Wed's class. Make SURE you understand the answer. If not, ask a TA. Once you are solid on a,b,c - start on question d. a) int count = 0; // initialization while( count < 5 ) { // condition System.out.println(count); // body count++; // same as count = count + 1; update } count 0,1,2,3,4,5 Output 0 1 2 3 4 b) int count = 0; while( count < 5 ) { count++; System.out.print(count + " "); } count 0,1,2,3,4,5 Output 1 2 3 4 5 c) int count = 0; while( count < 5 ) { System.out.println(count); } infinite loop What is the output from the following code segments? d) int count = 0; do { System.out.println(count); count++; } while( count < 0 ); System.out.println( "count after loop = " + count); 0 count after loop = 1 e) int count = 0; while( count < 0 ) { System.out.println(count); count++; } System.out.println( "count after loop = " + count); count after loop = 0 f) String str = "aabcaddjoabla"; int count = 0; int index = 0; while( index < str.length() ) { if( 'a' == str.charAt(index)) { count++; } index++; } System.out.println(count); 5 This program count the number of times the letter 'a' appears in the string. g) What is wrong with the following code segment? String str = "aabcaddjoabla"; int count = 0; int index = 0; while( index < str.length() ) { if( 'a' == str.charAt(index)) { count++; } else index++; } System.out.println(count); When the character 'a' is seen, the loop update (index++) is not performed. Compare to part f above.