0% found this document useful (0 votes)
95 views

Introduction To C++: Chapter 05: Looping Additional

The document discusses different types of loops in C++ including while, do-while, and for loops. It provides examples of how each loop can be used, such as using a while loop for input validation by repeating until valid data is entered. It also discusses concepts like user control of loops, using do-while loops for menus, sentinels to indicate the end of data input, and using break and continue statements to control loop execution.

Uploaded by

Aljawad14
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views

Introduction To C++: Chapter 05: Looping Additional

The document discusses different types of loops in C++ including while, do-while, and for loops. It provides examples of how each loop can be used, such as using a while loop for input validation by repeating until valid data is entered. It also discusses concepts like user control of loops, using do-while loops for menus, sentinels to indicate the end of data input, and using break and continue statements to control loop execution.

Uploaded by

Aljawad14
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

1

INTRODUCTION TO
C++
Chapter 05: Looping additional

While Loop for Input Validation


The while loop can be used to create routines
that repeat until acceptable data is entered
cout << "Enter a number (1-100) and"
<< " I will guess it. ";
cin >> number;
while (number < 1 || number > 100)
{ cout << "Number must be between 1 and 100."
<< " Re-enter your number. ";
cin >> number;
}
// Code to use the valid number goes here
5-2

User Control the Loop

Program can be written so that user input


determines loop repetition. User is prompted
before loop. Their input is used to control
number of repetitions

Example:
int num, limit;
cin >> limit;
num = 1;
while (num <= limit)
{
cout << num << << num*num << endl;
num++;
}
5-3

User Control the Loop


4

Example:
int num = 1, limit;
cout <<Enter a limit number: ;
cin >> limit;

while (num <= limit)


{
cout <<Enter a number: ;
cin >> num;
num *= num;
}

Using do while with menus


5

The do-while loop is a good choice for


repeating menu.
Example:
do {
cout << 1. Subject 1;
cout << 2. Subject 1;
cout << 3. Quit;
cin >> choice;
. // other code implementation
} while ( choice != 3);

Program is repeating until user enter


3

do while for Input Validation


6

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

sentinel: value in a list of values that indicates


end of data

Special value that cannot be confused with a


valid value, e.g., -999 for a test score

Used to terminate input when user may not


know how many values will be entered

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

Breaking Out of a Loop

Can use break to terminate execution of a


loop
Use sparingly if at all makes code harder to
understand
When used in an inner loop, terminates that
loop only and returns to the outer loop

5-9

Using break statement


10

int x = 0
while( x < 6)
{
x++;
if ( x == 3)
break;
cout << x << ;
}

5.10 The continue Statement

Can use continue to go to end of loop and


prepare for next repetition
while and do-while loops go to test and
repeat the loop if test condition is true
for loop goes to update step, then tests, and
repeats loop if test condition is true

Use sparingly like break, can make


program logic hard to follow

5-11

12

Using while and continue


statement
int x = 0
while( x < 6)
{
x++;
if ( x == 3)
continue;
cout << x << ;
}

Using for loop and continue


statement
13

for(int x = 0; x < 6; x++){


if ( x == 3)
continue;
cout << x << ;
}

14

Using continue and break


statement
int x = 0;
while( x < 6)
{
x++;
if ( x == 5 )
break;
if ( x == 3 )
continue;
cout << x << " " ;
}

5.11 Deciding Which Loop to Use

while: pretest loop (loop body may not be


executed at all) validating input, sentinel
value

do-while: post test loop (loop body will


always be executed at least once) repeating
a menu

for: pretest loop (loop body may not be


executed at all); has initialization and update
code; is useful with counters or if precise
number of repetitions is known
5-15

You might also like