Beruflich Dokumente
Kultur Dokumente
While)
Slide 1: Lesson Outline
This document is designed to help you take notes while watching the videos for this
lesson. Print these slides before going to the Video Lectures.
Note: Reviewing this material is not sufficient to fully understand the topic; you should
also complete the readings and exercises.
Review these slides
Slide 2: While
Quick Review:
Types of Statements Seen to Date
1. Declaration statement
Ex:
2. Declaration/Initialization statement
Ex:
3. Input/Output statement
Ex:
4. Assignment statement
Ex:
5. Selection statement
Ex:
Lesson 5
Slide 3:
3 While
Shamp
poo Algor
rithm:
Version 1
What is
s wrong with
w
this allgorithm?
Wet
Hair
Pour
Lather
Rinse
Repeat
Dry Hairr
Lesson 5
Slide 4:
4 While
Shamp
poo Algor
rithm: Ve
ersion 2
Better
but whatt is wrong
g with this
s algorithm
m?
Wet Hair
while Ha
air is dirty
Pour Sha
ampoo
Lather
Rinse
Dry Hairr
Slide 5:
5 While
Shamp
poo Algor
rithm:
Version 3
_________________
Wet Hair
while (H
Hair is dirty and _ _ _ _ _ _ _ _ _))
Pour Sha
ampoo
Lather
Rinse
__________=______________
Dry Hairr
Check video for ansswer.
Lesson 5
6: While
While Loop
p
ogical expre
ession) // lo
oop test
ody>
nt
le 1:
(n <= 3)
ut << "te
est " << n << end
dl;
= n + 1;
< "So lo
ong";
7: While
While Loop
p (cont'd)
At the starrt of each lo
oop iteratio
on, the logiical expresssion (condition) is
ed.
8: While
While Loop
p (cont'd)
If the cond
dition is tru
ue, the code in the loo
oop body iss executed aand program
automaticallly returns to
t the loop
p test (cond
dition) to e
evaluate the
e logical
on.
9: While
While Loop
p (cont'd)
If the cond
dition is fallse, the loo
op process tterminates and progra
am control
to the next statement that is the first state
ement afte
er the loop
p.
10: While
e
While Loop
p (cont'd)
ulary
ment includ
des a logica
al expressio
on, called th
he loop tesst, that tessts whether the
should conttinue and a loop body
y that defin
nes a task tthat is repe
eated.
execution of
o the task is called an
n iteration
n of the loop.
of one or more
m
loop control ob
bjects are used to determine the
e number o
of
tions.
value of the
e control ob
bjects is upd
dated in thee loop bodyy.
ile loop can
n be executted 0, 1 or many
m
timess.
Slide 11:
1 While
e
The wh
hile Loop
p (cont'd)
)
Examplle 2:
A while loop assign
ns a PASS/F
FAIL grade to
t four stud
dents. A sco
ore of at le
east 70 is
required
d for a passs.
studen
ntNo = 1;
while (student
tNo <= 4)
{
// in the lo
oop body, read a score an
nd give a grade
cin >> score
e;
= 70)
if (score >=
cout
c
<< "PASS" <<
< endl;
else
cout
c
<< "FAIL" <<
< endl;
// update
u
th
he value of stude
entNo
stud
dentNo++;
}
Slide 12:
1 While
e
The While
W
Loop
p (cont'd)
A countter-contro
olled loop performs a fixed numb
ber of itera
ations.
The lo
oop test de
etermines when
w
the loo
op has finisshed executting.
The number
n
of iterations iss determine
ed before beeginning th
he loop.
Lesson 5
The loop test looks at an event condition that specifies when the loop should
terminate.
Lesson 5
Lesson 5
17: While
e
Examples
s (cont'd)
)
le 6:
ill the follow
wing code fragment
f
ou
utput?
(a > 0)
m = 5;
ut << num
m <<endl;
< "Value
e of a is
s" << a;
18: While
e
Examples
s (cont'd)
)
le 7:
he code and
d identify th
he output. Assuming
A
ut is 2aZ?zv
v
t(c);
(c!= '\n
n' )
c == 'Z' || c ==
= 'z')
c = '*';
if (c >=
> 'A' &&
& c < 'Z' || c >=
= 'a' && c < 'z')
)
c++;
<< c;
get(c);
10
0
The do/while statement places the loop test at the end of the loop body.
Form:
do
<loop body>
while (logical expression) ; // loop test
Lesson 5
11
22: Do...W
While
Do...While vs. W
While
POST-TES
ST loop (exiit-condition); PRE-TE
EST loop (e
entry-condittion);
The loopin
ng condition
n is tested
after execcuting the lo
oop body;
The loo
oping condiition is teste
ed
before executing tthe loop bo
ody;
23: Do...W
While
he expressio
on is tested
d and found
d to be falsee, the loop is exited and control
to the state
ement that follows
f
the do-while sttatement.
12
2
Lesson 5
13
Lesson 5
14
Lesson 5
15