to repeat a set of 0 or more statements 0 or more times as long as some condition is true While Loop Syntax w hile conditionalExpression oneStatement w here oneStatement ID: 723849
Download Presentation The PPT/PDF document "while Loops While Loop Purpose:" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
while LoopsSlide2
While Loop
Purpose:
to repeat a set of 0 or more statements
0 or more times
as long as some condition is true.Slide3
While Loop
Syntax:
w
hile (
conditionalExpression
)
oneStatement
w
here
oneStatement
is exactly one statement with a semicolon, or a blockSlide4
While Loop
Semantics:
At the beginning:
conditionalExpression
is evaluated and
- If true:
oneStatement
is executed, then control
returns to the beginning.
- If false:
oneStatement
skipped.Slide5
While Loop
Style #1: one statement
w
hile (
conditional
)
Exactly one statement
; // indent the 1 statement
Style #2:
block
while (
conditional
)
{
// line up with
while
statements
//
indent statements
}
//
line up with
whileSlide6
While Loop
Quick Vocabulary
Body (of the loop):
the one statement or block
Iteration:
one execution of the loop
(one time through, one “lap”)
Slide7
While Loop
Example 1: only syntax counts!
int
a = 1;
w
hile (a <=
3
)
Prints:
a++;
4
cout
<< a <<
endl
;Slide8
While Loop
Example 2: careful of where begins/ends
int
a = 1;
w
hile (a <=
3
)
{
Prints:
a++;
2
cout
<< a <<
endl
;
3
}
4Slide9
While Loop
Example 3: 0 iterations possible
int
a = 12;
w
hile (a <=
3
) {
Prints:
a++;
Done!
cout
<< a <<
endl
;
}
c
out
<< ”Done!\n”;
Slide10
While Loop Strategies
Counter-Controlled Loop
int
i
=
start
;
w
hile (
i
<=
stop
) {
// do something some number of times
i
+=
increment
;
//
i
=
i
+
increment
;
}
Slide11
While Loop Strategies
Counter-Controlled Loop
Example: start at 1, stop at 20, count by 2's
int
i
=
1
;
w
hile (
i
<=
20
) {
cout
<<
i
<< ” ”;
i
+=
2
;
}
// prints: 1 3 5 7 9 11 13 15 17 19
Slide12
While Loop Strategies
User Input Validation
// answer could be float/string/etc.
int
answer =
wrong_answer
;
w
hile (
answer is wrong
) {
cout
<< ”Enter …”;
cin
>> answer;
}
Slide13
While Loop Strategies
User Input Validation
int
answer = 0;
w
hile (answer < 1 || answer > 10) {
cout
<< ”Enter integer 1 to 10: ”;
cin
>> answer;
}
Slide14
While Loop Strategies
User Input Validation
string answer = ””;
w
hile (answer !=”left” && answer!=”right”) {
cout
<< ”Enter left or right: ”;
cin
>> answer;
}
Slide15
While Loop Strategies
User Input Validation with error message
// ask first time before the loop
string answer;
cout
<< ”Enter left or right: ”;
cin
>> answer
;
w
hile (answer !=”left” && answer!=”right”) {
cout
<< “Error! Type left or right!!\n”;
cout
<< ”Enter left or right: ”;
cin
>> answer;
}
Slide16
While Loop Strategies
User Input Validation with error message
// use
if
to see if we need an error message
string answer = ””;
w
hile (answer !=”left” && answer!=”right”) {
cout
<< ”Enter left or right: ”;
cin
>> answer;
if (answer
!=”left” && answer!=”right
”)
cout
<< ”Invalid answer!\n”;
}
Slide17
While Loop Strategies
Summation of entered Values
i
nt
num
,
sum = 0;
w
hile (
whatever_control
) { // any type of loop
cout
<< ”Enter number: ”;
cin
>>
num
;
sum +=
num
;
// sum = sum +
num
}
Slide18
While Loop Strategies
Counting Strategy
i
nt
num
,
count = 0;
w
hile (
whatever_control
) {
cout
<< ”Enter number: ”;
cin
>>
num
;
count++;
// count = count + 1
}
cout
<< count << " numbers entered";
Slide19
While Loop Strategies
Average of Inputs: Count + Sum
float
num
,
sum = 0
, average;
i
nt
count = 0
;
while (
whatever_control
) {
cout
<< ”Enter number: ”;
cin
>>
num
;
count++;
// count = count + 1
sum +=
num
;
// sum = sum +
num
}
average = sum / count;Slide20
While Loop Strategies
Mini - find the lowest value
int
min
=
ridiculously
High
number
;
while
(
whatever_control
) {
cout
<< ”Enter number: ”;
cin
>>
num
;
if (
num
< min)
min =
num
;
} Slide21
While Loop Strategies
Max - find the highest value
int
max
=
ridiculously
Low
number
;
while
(
whatever_control
) {
cout
<< ”Enter number: ”;
cin
>>
num
;
if (
num
>
max)
max =
num
;
} Slide22
Sentinel Controlled Loop
Motivation/Purpose:
- programs must often input data
serially
(one at a time).
- the
Number of Inputs
may not be known:
when the program is written
when the program is executing and is ready to input the first data value
How many data items will there be???Slide23
Sentinel Controlled Loop
Definition of a Sentinel Value:
a value entered as data,
but
is not processed as data;
instead
, it
signals the end
of data
.
Characteristics:
choosing a Sentinel Value
- must be the
same Data Type
as the data
- should not "make sense" as dataSlide24
Sentinel Controlled Loop
General Pattern:
Input data
while (data != sentinel) {
process data
Input data
}
Note the check for the sentinel is done
immediately
after a value is inputted.Slide25
Sentinel Controlled Loop
Example:
Enter an unknown number of test scores and calculate the sum. Use -1 as the Sentinel.
Note: the value
-1
- is the same type (
int
) as a test score
- "doesn't make sense" as a test scoreSlide26
Sentinel Controlled Loop
int
score, sum=0;
c
out
<< "Enter score, -1 to quit: ";
cin
>> score;
while (score != -1) {
sum += score;
cout
<< "Enter score, -1 to quit: ";
cin
>> score;
}
cout
<< "The sum is " << sum <<
endl
;Slide27
Sentinel Controlled Loop
Origin of the word
Sentinel:
- a
guard
on the "lookout" for something
(usually the "bad guys" or other mischief)
The while() is "on the lookout" for a
particular value, and reacts (stops) when
it sees the value. The value is not treated
as "ordinary".Slide28
Vocabulary
Term
Definition
Body
the one statement or block of statements repeated by a loop.
Iteration
one execution of a loop (lap).
Sentinel
a value entered as data, but is not processed
as data; instead, it signals the end of data.