Overview of Loops
-
Upload
elaine-hunt -
Category
Documents
-
view
26 -
download
0
description
Transcript of Overview of Loops
![Page 1: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/1.jpg)
Overview of Loops
![Page 2: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/2.jpg)
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
}
![Page 3: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/3.jpg)
Sentinel-controlled Loop
Sop (“Enter a score (-1 to quit)”);score = kb.nextInt(); // “priming” readwhile (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}
![Page 4: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/4.jpg)
Count-controlled Loops
int i = 1; // initializationwhile (i <= 10) // test{
Sop ("i = " + i); // action(s)...
i++; // increment}
for ( int i=1; i<=10; i++ ){
Sop ("i = " + i); // action(s)
.
.
.}
![Page 5: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/5.jpg)
When to use while vs for?
while• When you don’t know ahead of
time how many times something needs to happen
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
![Page 6: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/6.jpg)
We have 3 files that contain a list of golf scores that we want to average.
golf1.txt
7865827091887568
golf2.txt
7865827091887568-1
golf3.txt
8 7865827091887568
File has scores only File has sentinel value First number in file tells us number of scores on file
![Page 7: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/7.jpg)
golf1.txt (has scores only) – use an “end of file loop”
int score;int total = 0;int numScores = 0;double avg;
while (infile.hasNext()){ score = infile.nextInt(); total = total + score; numScores++;}
avg = (double) total / numScores;SOP (avg);
golf1.txt
7865827091887568
![Page 8: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/8.jpg)
golf2.txt (has sentinel value) – use a “sentinel-controlled loop”
int score;int total = 0;int numScores = 0;double avg;
score = infile.nextInt();while (score != -1){ total = total + score; numScores++; score = infile.nextInt();}
avg = (double) total / numScores;SOP (avg);
golf2.txt
7865827091887568-1
![Page 9: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/9.jpg)
golf3.txt (has number of scores at beginning of file)version 1 – use a “count-controlled” while loop
int score;int total = 0;int numScores;double avg;int count = 0;
numScores = infile.nextInt();while (count < numScores){ score = infile.nextInt(); total = total + score; count++;}
avg = (double) total / numScores;SOP (avg);
golf3.txt
8 7865827091887568
![Page 10: Overview of Loops](https://reader036.fdocuments.in/reader036/viewer/2022082710/56812cf7550346895d91c393/html5/thumbnails/10.jpg)
golf3.txt (has number of scores at beginning of file)version 2 – use a “count-controlled” for loop
int score;int total = 0;int numScores;double avg;
numScores = infile.nextInt();for (int i=0; i < numScores; i++){ score = infile.nextInt(); total = total + score;}
avg = (double) total / numScores;SOP (avg);
golf3.txt
8 7865827091887568