1 / 10

Overview of Loops

Overview of Loops. End of File Loop. while ( infile.hasNext ()) // true if more unread data on file { score = infile .nextInt (); // now input is at beginning of loop // actions }. Sentinel-controlled Loop. Sop (“Enter a score (-1 to quit )”);

elaine-hunt
Download Presentation

Overview of Loops

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. Overview of Loops

  2. End of File Loop while (infile.hasNext()) // true if more unread data on file { score = infile.nextInt(); // now input is at beginning of loop // actions }

  3. Sentinel-controlled Loop Sop (“Enter a score (-1 to quit)”); score = kb.nextInt(); // “priming” read while (score != -1) // sentinel value is -1 { // actions Sop (“Enter another score (-1 to quit)); score = kb.nextInt(); // remember to read another value at end of loop }

  4. Count-controlled Loops inti = 1; //initialization while (i <= 10) //test { Sop ("i = " + i); // action(s) . . . i++; // increment } for ( inti=1; i<=10; i++ ) { Sop ("i = " + i); // action(s) . . . }

  5. When to use while vs for? while for When you do know ahead of time how many times something needs to happen Note: could use while in this situation but most programmers prefer for • When you don’t know ahead of time how many times something needs to happen

  6. golf2.txt 78 65 82 70 91 88 75 68 -1 golf3.txt 8 78 65 82 70 91 88 75 68 golf1.txt 78 65 82 70 91 88 75 68 We have 3 files that contain a list of golf scores that we want to average. First number in file tells us number of scores on file File has scores only File has sentinel value

  7. golf1.txt (has scores only) – use an “end of file loop” int score; int total = 0; intnumScores = 0; double avg; while (infile.hasNext()) { score = infile.nextInt(); total = total + score; numScores++; } avg = (double) total / numScores; SOP (avg); golf1.txt 78 65 82 70 91 88 75 68

  8. golf2.txt (has sentinel value) – use a “sentinel-controlled loop” golf2.txt 78 65 82 70 91 88 75 68 -1 int score; int total = 0; intnumScores = 0; double avg; score = infile.nextInt(); while (score != -1) { total = total + score; numScores++; score = infile.nextInt(); } avg = (double) total / numScores; SOP (avg);

  9. golf3.txt (has number of scores at beginning of file)version 1 – use a “count-controlled” while loop golf3.txt 8 78 65 82 70 91 88 75 68 int score; int total = 0; intnumScores; double avg; int count = 0; numScores = infile.nextInt(); while (count < numScores) { score = infile.nextInt(); total = total + score; count++; } avg = (double) total / numScores; SOP (avg);

  10. golf3.txt (has number of scores at beginning of file)version 2 – use a “count-controlled” for loop golf3.txt 8 78 65 82 70 91 88 75 68 int score; int total = 0; intnumScores; double avg; numScores = infile.nextInt(); for (inti=0; i < numScores; i++) { score = infile.nextInt(); total = total + score; } avg = (double) total / numScores; SOP (avg);

More Related