Upload
azrinshah-abu-bakar
View
230
Download
0
Embed Size (px)
Citation preview
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
1/56
Chapter 4b
Repetition Control Structure
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
2/56
The Objectives
1. To use while, do-while, and for loops toexecute statements repeatedly.
2. To understand the o! o" control in loops.
#. To use $oolean expressions to control loops.4. To !rite nested loops.
%. To &no! the similarities and di'erences o"three types o" loops.
(. )Optional* To implement pro+ram control!ith break and continue.
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
3/56
hy is Repetition -eeded
• /bility to repeat an operation or a serieso" operations many times.
• /n action or a series o" actions.
• C00 has three repetition , or loopin+structures that allo! us to repeat a set o"statements until certain conditions aremet• while
• do-while
• for
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
4/56
while oopin+ Structure4
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
5/56
while Loop
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
6/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
9nitiali:e count
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
7/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
)count 3 2* is True
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
8/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
;rint elcome to C00
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
9/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
9ncrease count by 1-O count is 1
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
10/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
)count 3 2* is still truesince count is 1
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
11/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
;rint elcome to C00
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
12/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
9ncrease count by 1-O count is 2
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
13/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
)count 3 2* is "alsesince -O count is 2
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
14/56
Trace whileoop
int count = 0;
!hile )count 3 2*
count 33 5elcome to C0067
count007
8
The loop exits.
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
15/56
Controllin+ while oop
• hen a pro+ram needs to be tested a+ain anda+ain on certain data it !ill operate a lar+eamount o" data.
•
To overcome the problem, !e have "our cases Counter controlled !hile loops
Sentinel controlled !hile loops
=la+ controlled !hile loops
>ser controlled !hile loops
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
16/56
Counter-controlled whileloops
• e &no! ho! many pieces o" data need to be read.
• >se a counter, by initiali:in+ the value to @.
• 9" counter 3 -, the body o" the !hile statementexecutes until the value o" counter AB -
• The structure
counter = 0; //initializethe loop control variable
while (counter< N) //testthe loop control variable
{…
…
counter ++ //updatethe loop control variable
…
}
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
17/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
18/56
Output
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
19/56
Sentinel?Controlled whileloops
• e do not &no! ho! many data needs to be read.
• The last entry called sentinel, is a special value.
• / sentinel value is a value that is not a le+itimate data value"or a particular problem )but is o" proper type* that is used as
a 5stoppin+6 value.• The !hile loop continues to execute as lon+ as the pro+ram
has not read the sentinel.
• Feneral structure cin variable;
while (variable != "#)
{
…
cinvariable;
…
}/entinel alue
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
20/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
21/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
22/56
>ser Controlled while oops
• >ses a user predeGned variable tocontrol the loop
char continueoop B HIH7
!hile )continueoop BB HIH*
JJ
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
23/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
24/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
25/56
=la+?Controlled while oops
• >ses a bool variable to control the loop
•
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
26/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
27/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
28/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
29/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
30/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
31/56
doAwhile oopin+ Structure
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
32/56
doAwhile Loop
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
33/56
doAwhile oop
Ninclude 3iostreamA
usin+ namespace std7
int main)*
int a7JJinitiali:ation
do
cout 33 KTo stop enter a number bet!een 1@ 2@ K7
cin AA a7 JJupdatin+8 !hile )a 3 1@ a A 2@*7 JJcondition
cout 33K
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
34/56
Output
To stop enter a number bet!een 1@ 2@ #
To stop enter a number bet!een 1@ 2@ 1@
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
35/56
for oopin+ Structure
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
36/56
for Loop
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
37/56
Trace for oop
int i
"or )iB@7 i327 i00*
cout 33 5elcome to C00678
declare i
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
38/56
Trace for oop
int i
"or )i=07 i327 i00*
cout 33 5elcome to C00678
execute
initiali:er-O i is @
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
39/56
Trace for oop
int i
"or )iB@7 i
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
40/56
Trace for oop
int i
"or )iB@7 i327 i00*
cout 33 5elcome to C00678
;rintelcome to C00
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
41/56
Trace for oop
int i
"or )iB@7 i327 i,,*
cout 33 5elcome to C00678
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
42/56
Trace for oop
int i
"or )iB@7 i
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
43/56
Trace for oop
int i
"or )iB@7 i327 i00*
cout 33 5elcome to C00678
;rintelcome to C00
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
44/56
Trace for oop
int i
"or )iB@7 i327 i,,*
cout 33 5elcome to C00678
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
45/56
Trace for oop
int i
"or )iB@7 i
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
46/56
Trace for oop
int i
"or )iB@7 i327 i00*
cout 33 5elcome to C00678
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
47/56
Comparin+ for and whileoops
C i f d hil
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
48/56
Comparin+ for and whileoops
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
49/56
Comparing while dowhile for loops
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
50/56
Decommendations
• >se the one that is most intuitive andcom"ortable "or you.
• 9n +eneral, a for loop may be used i" thenumber o" repetitions is &no!n, as, "or example,!hen you need to print a messa+e 1@@ times.
• / while loop may be used i" the number o"repetitions is not &no!n, as in the case o"readin+ the numbers until the input is @.
• / do-while loop can be used to replace a !hileloop i" the loop body has to be executed be"oretestin+ the continuation condition.
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
51/56
-ested for oop
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
52/56
Other Statement Related to oopin+
• break causes a loop to terminate
terminate only the inner loop in nested
loop• continue
trans"ers to the testin+ expression in
!hile and doU!hile statement trans"ers to the updatin+ expression in a
"or statement.
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
53/56
The continue Statement
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
54/56
The break
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
55/56
8/19/2019 BTKR1343 - Chapter 4b - Control Technique -Repetition
56/56
Output