Class 13 M 2/25 Comp 110 Read Ch 5.1 for Wed Optional review session Thr 2/28 time TBD Loops 2 patterns for single loops practice problems Loop syntax is the required grammatical form for the Java loops: while for do-while. A loop pattern is how the loop is used. How does it terminate? How many times does it execute? I. Single Loop Patterns 1. Loop until told to stop. When we first enter the loop, we have no idea how many times the loop will execute. It all depends on the data provided to the loop. 2. Fixed number of executions. When we first enter the loop, we know exactly how many times the loop will execute. Consider the problem of reading grades and averaging them. The inner loop of listing 4.4 is of the first type. The loop keeps reading numbers until a negative number is found - this is a sentinel value that signals the end of the data and terminates the loop. Input data is of the form 99.0 86.5 97.0 -1 while and do-while are most appropriate for pattern 1. We could also average grades using loop pattern 2. The first line of data tells the program how many grades will follow. Input data is of the form 3 99.0 86.5 97.0 and the code is final int NUM_GRADES = keyboard.nextInt(); double sum = 0.0; for( int index = 0; index < NUM_GRADES; index++ ) { double grade = keyboard.nextDouble(); sum = sum + grade; } System.out.println("average = " + sum / NUM_GRADES); Unlike sum and index, NUM_GRADES is a constant not a variable. It is assigned a value only once then it never changes. 'final' tells Java that it won't be changed. Using ALL CAPS reminds that human reader that it is a constant (variable names are never all caps). Test preparation exercises 1) What is the output from the following code segment? int sum = 0; for( int i = 0; i < 2; i++ ) { sum = sum + i; for( int j = 2; j > 0; j-- ) sum = sum + j; System.out.println(" sum = " + sum ); } Trace the sequence of values assigned to sum, i, and j. Start with sum 0, 0, 2, 3 i 0, 1 j 2, 1, 0 ******** correct answer sum = 3 sum = 2) Consider the following fragment of code that is based on boolean variables a and b. if( a || b ) if( !b ) System.out.println("one"); else if( !a ) System.out.println("two"); else System.out.println("three"); Give values for a and b that make the code fragment display: i) one ii) two iii) three If there is not a combination of values that will produce the indicated output, write "impossible". ****** correct answer i) a true b false ii) a false b true iii) a true b true Step 1 - show the logical structure of the code. Work from the bottom up matching each 'else' with the appropriate 'if'. On paper, I draw a line connecting the two. An alternative is to rewrite the code using extra braces and proper indentation. if( a || b ) { if( !b ) System.out.println("one"); else { if( !a ) System.out.println("two"); else System.out.println("three"); } } Step 2 - For each of the outputs, trace backwards to deduce the required values of a and b. To reach SOP("one"), !b must be true so b must be false. To reach the nested "if( !b )...", a || b must be true. Since b is false, a must be true. Final answer: a true, b false Step 3 - You are not done. Check your result by tracing the code in the forward direction. If a and b have the values you specified, will the code actually print "one"? If so, you are done. If not, you have made a mistake somewhere