chapter 05 additionac++l
Post on 02-Jun-2018
220 Views
Preview:
TRANSCRIPT
-
8/10/2019 Chapter 05 AdditionaC++l
1/15
INTRODUCTION TOC++
Chapter 05: Looping additional
1
-
8/10/2019 Chapter 05 AdditionaC++l
2/15
While Loop for Input Validation
The while loop can be used to create routinesthat repeat until acceptable data is entered
cout number;
while (number < 1 || number > 100)
{ cout number;
}
// Code to use the valid number goes here
5-2
-
8/10/2019 Chapter 05 AdditionaC++l
3/15
User Control the Loop
Program can be written so that user inputdetermines loop repetition. User is promptedbefore loop. Their input is used to controlnumber of repetitions
Example:
int num, limit;cin >> limit;
num = 1;while (num
-
8/10/2019 Chapter 05 AdditionaC++l
4/15
User Control the Loop4
Example:
int num = 1, limit;cout limit;
while (num
-
8/10/2019 Chapter 05 AdditionaC++l
5/15
Using do while with menus5
The do-while loop is a good choice for
repeating menu.
Example:
do {
cout
-
8/10/2019 Chapter 05 AdditionaC++l
6/15
do while for Input Validation6
Example:
char continue = y; //default value
do {
// other code implementation
cout> continue;
} while ( continue == y || continue == Y);
-
8/10/2019 Chapter 05 AdditionaC++l
7/15
5.8 Sentinels
sentinel: value in a list of values that indicates
end of data
Special value that cannot be confused with avalid value, e.g., -999for a test score
Used to terminate input when user may not
know how many values will be entered
5-7
-
8/10/2019 Chapter 05 AdditionaC++l
8/15
Sentinel Example
int total = 0;cout points;
while (points != -1) // -1 is the sentinel{
total += points;cout > points;
}
5-8
-
8/10/2019 Chapter 05 AdditionaC++l
9/15
Breaking Out of a Loop
Can usebreakto terminate execution of a
loop
Use sparingly if at all
makes code harder tounderstand
When used in an inner loop, terminates that
loop only and returns to the outer loop
5-9
-
8/10/2019 Chapter 05 AdditionaC++l
10/15
Using break statement10
int x = 0
while( x < 6)
{
x++;
if ( x == 3)
break;
cout
-
8/10/2019 Chapter 05 AdditionaC++l
11/15
5.10 The continueStatement
Can use continueto go to end of loop andprepare for next repetition
whileand do-whileloops go to test and
repeat the loop if test condition is true forloop goes to update step, then tests, and
repeats loop if test condition is true
Use sparingly likebreak, can make
program logic hard to follow
5-11
-
8/10/2019 Chapter 05 AdditionaC++l
12/15
Using while and continue
statement12
int x = 0
while( x < 6)
{
x++;
if ( x == 3)
continue;
cout
-
8/10/2019 Chapter 05 AdditionaC++l
13/15
Using for loop and continue
statement13
for(int x = 0; x < 6; x++){
if ( x == 3)
continue;
cout
-
8/10/2019 Chapter 05 AdditionaC++l
14/15
Using continue and break
statement14
int x = 0;
while( x < 6)
{
x++;
if ( x == 5 )break;
if ( x == 3 )
continue;
cout
-
8/10/2019 Chapter 05 AdditionaC++l
15/15
5.11 Deciding Which Loop to Use
while: pretest loop (loop body may not beexecuted at all) validating input, sentinelvalue
do-while: post test loop (loop body willalways be executed at least once) repeatinga menu
for: pretest loop (loop body may not beexecuted at all); has initialization and updatecode; is useful with counters or ifprecisenumber of repetitions is known
5-15
top related