11 Loops PDF
11 Loops PDF
Loops
Loops
False …
Condition Tr
ue
Statement1
2 Loops - Struble
1
Loops in C++
while (Expression)
Statement
! Semantics
– Executes Statement as long as Expression evaluates to true
3 Loops - Struble
4 Loops - Struble
2
While Loop (Example)
const int ONE_DOLLAR = 100; // one dollar in cents
5 Loops - Struble
Kinds of Loops
6 Loops - Struble
3
Event Controlled Loop (Example)
int high = 20;
int low = 0;
cout << "Low: " << low << "High: " << high << endl;
7 Loops - Struble
cout << "Enter the number of grades to read: " << flush;
cin >> numGrades;
while (numRead < numGrades)
{
cin >> grade;
total = total + grade;
numRead = numRead + 1;
}
if (numRead > 0)
cout << "Your average is " << (total * 1.0) / numRead
<< endl;
8 Loops - Struble
4
Sentinel Controlled Loop
const int SENTINEL = -1; 87 99 82 74 83 88 90 -1 80
int numRead = 0;
int grade = 0, total = 0;
cout << "Enter grades or " << SENTINEL << " to quit." << endl;
cin >> grade; // priming read
while (grade != SENTINEL)
{
total = total + grade;
numRead = numRead + 1;
cin >> grade; // read the next grade
}
if (numRead > 0)
cout << "Your average is " << (total * 1.0) / numRead
<< endl;
9 Loops - Struble
Exercises
! Write a loop to do the following:
1. Read in 10 integers.
2. Find the maximum value.
3. Find the minimum value.
! Hints
– Don't store all 10 values at once, calculate the
maximum/minimum so far as you go.
– Use INT_MIN and INT_MAX to initialize maximum/minimum
value so far.
! Trace using the following input.
30 -209 45 827 -93 101 -445 79 827 83
10 Loops - Struble
5
Input Failure
11 Loops - Struble
12 Loops - Struble
6
Input Failure (If Example)
13 Loops - Struble
14 Loops - Struble
7
Read until Input Failure
(Correct Example)
const char DELIMITER = '|';
string name = ""; Joe Missouri|32
int age = -1; Sally White|27
Missy Green|24
ifstream In("Data.txt");
15 Loops - Struble
ifstream In("Data.txt");
cout << "Name: " << name << "\tAge: " << age << endl;
}
16 Loops - Struble
8
Recovering from Input Failure
17 Loops - Struble
18 Loops - Struble
9
Exercise
19 Loops - Struble
End-Of-File Handling
InputStreamVariable.eof()
20 Loops - Struble
10
End-Of-File Example
int anInt = 0; Input
char c = '\0';
ifstream In("Input.txt"); 1 With newline
2
In >> anInt; 3
In.get(c);
while (!In.eof())
{
cout << anInt << endl; 1 Without newline
2
In >> anInt;
3
In.get(c);
}
21 Loops - Struble
Other Loops
22 Loops - Struble
11
For Loops
! A for loop is the preferred syntax for count controlled
loops.
! Syntax
for (Initialization; TestExpression; Update)
Statement
23 Loops - Struble
24 Loops - Struble
12
Increment and Decrement
25 Loops - Struble
cout << "Enter the number of grades to read: " << flush;
cin >> numGrades;
for (numRead = 0; numRead < numGrades; numRead++)
{
cin >> grade;
total = total + grade;
}
if (numRead > 0)
cout << "Your average is " << (total * 1.0) / numRead
<< endl;
26 Loops - Struble
13
For Loop and Decrement (Example)
const char STAR='*'; // what to draw
int numPrint;
cout << "How many stars do you want to print? " << flush;
for (cin >> numPrint; numPrint > 0; numPrint--)
cout << STAR;
27 Loops - Struble
Pitfall
cout << "How many stars do you want to print? " << flush;
for (cin >> numPrint; numPrint > 0; numPrint--)
cout << STAR;
cout << endl;
Only printing the stars
is in the loop.
28 Loops - Struble
14
Pitfall
cout << "How many stars do you want to print? " << flush;
for (cin >> numPrint; numPrint > 0; numPrint--);
{
cout << STAR;
} One star will
cout << endl; always be printed!
29 Loops - Struble
Do…While Loop
! Syntax True
do
Statement1
while (Condition);
30 Loops - Struble
15
Do…While Example
31 Loops - Struble
Do … While Example
! Do … While solution
do
{
cout << "Enter your age: ";
cin >> age;
if (age < 0)
cout << "Your age must be at least 0" << endl;
} while (age < 0);
The semicolon is
required here!
32 Loops - Struble
16
Exercises
33 Loops - Struble
Nested Loops
34 Loops - Struble
17
Other Control Statements
35 Loops - Struble
Break Statement
Statement1
break;
36 Loops - Struble
18
Break Example
37 Loops - Struble
Better implementation
const int SENTINEL = -1;
38 Loops - Struble
19
Continue Statement
Statement3 Statement1
continue;
39 Loops - Struble
Continue Example
! Total positive values
const int SENTINEL = 0; 1 -2 3 0 4 5 6
int valEntered = -1, total = 0;
while (valEntered != 0)
{
cout << "Enter a positive value or "
<< SENTINEL << " to quit: " << flush;
cin >> valEntered;
if (valEntered < 0)
continue;
total = total + valEntered;
}
40 Loops - Struble
20
Better Implementation
const int SENTINEL = 0;
int valEntered = -1, total = 0;
while (valEntered != 0)
{
cout << "Enter a positive value or "
<< SENTINEL << " to quit: " << flush;
cin >> valEntered;
if (valEntered > 0)
total = total + valEntered;
}
41 Loops - Struble
Continue Statement
42 Loops - Struble
21