180 likes | 287 Views
Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format). Two types of loops. count-controlled loops repeat a specified number of times event-controlled loops something happens inside the loop body that causes the repetition to stop.
E N D
Chapter 5 File Objects and LoopingStatements (Some slides have been modified from their original format)
Two types of loops count-controlled loops repeat a specified number of times event-controlled loops something happens inside the loop body that causes the repetition to stop
While Statement while ( Expression) { . . // loop body . } NOTE: Loop body can be a single statement, a null statement, or a block
When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body WHILE LOOP false Expression true body statement
Count-controlled Example int count; // Declare loop variable count = 1; // Initialize loop variable while (count <= 4) // Test expression { // Repeated action (iterations) System.out.println(“count is “ + count); count ++;// Update loop variable } System.out.println(“Done”);
count Count-controlled loop int count; count = 1; while ( count <= 4 ) { System.out.println ( “count is “ + count ); count++; } System.out.println(“Done”); OUTPUT
See step-by-step execution of count-controlled loop in separate file, loop.ppt.
Count-Controlled Loop Example • dataFile contains 100 blood pressures, one to a line • Use a while loop to read the 100 blood pressures and find their total
// Count-controlled loop int thisBP; int total; int count; count = 1; // Initialize total = 0; while (count <= 100) // Test expression { thisBP = Integer.parseInt(dataFile.readLine()); total = total + thisBP; count++; // Update } System.out.println(“The total = “ + total);
Event-controlled loops • Sentinel controlled Keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop • End-of-file controlled Keep processing data as long as there is more data in the file • Flag controlledKeep processing data until the value of a flag changes in the loop body
Examples of kinds of loops What kind of loop ? Read exactly 100 blood pressures from a file. Keep reading until a special (impossible) value is read. What kind of loop ? Read all the blood pressures from a file no matter how many are there. What kind of loop ? Read blood pressures until a dangerously high BP (200 or more) is read. What kind of loop ?
Examples of kinds of loops Count-controlled loop Read exactly 100 blood pressures from a file. Keep reading until a special (impossible) value is read. Sentinel-controlled loop Read all the blood pressures from a file no matter how many are there. End-of-file controlled loop Read blood pressures until a dangerously high BP (200 or more) is read. Flag-controlled loop
A sentinel-controlled loop // Sentinel is negative blood pressure. int thisBP; int total; int count; count = 1; // Initialize total = 0; // Priming read thisBP = Integer.parseInt(dataFile.readLine()); while (thisBP > 0) // Test expression { total = total + thisBP; count++; // Update thisBP = Integer.parseInt(dataFile.readLine()); } System.out.println(“The total = “ + total);
An end-of-file controlled loop // Read and sum until end of line int thisBP; int total; int count; count = 1; // Initialize total = 0; String line; line = dataFile.readLine(); while (line != null) // Test expression { thisBP = Integer.parseInt(line); total = total + thisBP; count++; // Update line = dataFile.readLine(); } System.out.println(“The total = “ + total);
count = 0; sum = 0; notDone = true; while ( notDone ) { line = dataFile.readLine( ); // Get a line if (line != null) // Got a line? { number = Integer.parseInt(line); if (number % 2 == 1) // Is number odd? { count++; sum = sum + number; notDone = ( count < 10 ); } } else // Reached EOF unexpectedly { errorFile.println(“EOF reached before ten odd values.”) notDone = false; // Change flag value } }
Pattern of a nested loop initialize outer loop while ( outer loop condition ) { . . . initialize inner loop while ( inner loop condition ) { inner loop processing and update } . . . }
Loop Testing and Debugging • Test data should test all sections of the program • Beware of infinite loops -- the program doesn’t stop • Check loop termination condition, and watch for an OBOB (off-by-1 bug) • Use algorithm walk-through to verify that appropriate conditions occur in the right places • Trace execution of loop by hand with code walk-through • Use a debugger (if available) to run program in “slow motion” or use debug output statements