Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)

Similar presentations


Presentation on theme: "1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)"— Presentation transcript:

1 1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)

2 2 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

3 3 While Statement while ( Expression ) {.. // loop body. } NOTE: Loop body can be a single statement, a null statement, or a block

4 4 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 true body statement Expression

5 5 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-controlled Example

6 6 Count-controlled loop int count; count = 1; while ( count <= 4 ) { System.out.println ( “count is “ + count ); count++; } System.out.println(“Done”); OUTPUT count

7 7 See step-by-step execution of count-controlled loop in separate file, loop.ppt.

8 8 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 Example

9 9 // 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);

10 10 Event-controlled loops l Sentinel controlled Keep processing data until a special value which is not a possible data value is entered to indicate that processing should stop l End-of-file controlled Keep processing data as long as there is more data in the file l Flag controlled Keep processing data until the value of a flag changes in the loop body

11 11 Examples of kinds of loops What kind of loop ?Read exactly 100 blood pressures from a file. 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 ? Keep reading until a special (impossible) value is read.

12 12 Examples of kinds of loops Count-controlled loop Read exactly 100 blood pressures from a file. End-of-file controlled loop Read all the blood pressures from a file no matter how many are there. Flag-controlled loop Read blood pressures until a dangerously high BP (200 or more) is read. Sentinel-controlled loop Keep reading until a special (impossible) value is read.

13 13 // 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); A sentinel-controlled loop

14 14 // 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); An end-of-file controlled loop

15 15 A flag-controlled loop

16 16 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 }

17 17 initialize outer loop while ( outer loop condition ) {... initialize inner loop while ( inner loop condition ) { inner loop processing and update }... } Pattern of a nested loop

18 18 Loop Testing and Debugging Test data should test all sections of the program l Beware of infinite loops -- the program doesn’t stop l Check loop termination condition, and watch for an OBOB (off-by-1 bug) l Use algorithm walk-through to verify that appropriate conditions occur in the right places l Trace execution of loop by hand with code walk-through l Use a debugger (if available) to run program in “slow motion” or use debug output statements


Download ppt "1 Chapter 5 File Objects and Looping Statements (Some slides have been modified from their original format)"

Similar presentations


Ads by Google