1 / 18

Chapter 5 File Objects and Looping Statements

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.

anahid
Download Presentation

Chapter 5 File Objects and Looping Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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. While Statement while ( Expression) { . . // loop body . } NOTE: Loop body can be a single statement, a null statement, or a block

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

  5. 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”);

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

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

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

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

  11. 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 ?

  12. 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

  13. 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);

  14. 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);

  15. A flag-controlled loop

  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. Pattern of a nested loop initialize outer loop while ( outer loop condition ) { . . . initialize inner loop while ( inner loop condition ) { inner loop processing and update } . . . }

  18. 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

More Related