Introduction To C++: Chapter 05: Looping Additional
Introduction To C++: Chapter 05: Looping Additional
INTRODUCTION TO
C++
Chapter 05: Looping additional
Example:
int num, limit;
cin >> limit;
num = 1;
while (num <= limit)
{
cout << num << << num*num << endl;
num++;
}
5-3
Example:
int num = 1, limit;
cout <<Enter a limit number: ;
cin >> limit;
Example:
char continue = y; //default value
do {
// other code implementation
cout << you want to continue? (y/n);
cin >> continue;
} while ( continue == y || continue == Y);
5.8 Sentinels
5-7
Sentinel Example
int total = 0;
cout << "Enter points earned "
<< "(or -1 to quit): ";
cin >> points;
while (points != -1) // -1 is the sentinel
{
total += points;
cout << "Enter points earned: ";
cin >> points;
}
5-8
5-9
int x = 0
while( x < 6)
{
x++;
if ( x == 3)
break;
cout << x << ;
}
5-11
12
14