Date post: | 07-Jul-2018 |
Category: |
Documents |
Upload: | forkenstein |
View: | 231 times |
Download: | 0 times |
of 43
8/18/2019 Repetition Cont
1/43
Repetition Structure
(continued)
8/18/2019 Repetition Cont
2/43
Counter-Controlled Loops
• Loops can be controlled using a
counter rather than a sentinel value
• These are called counter-controlled loops
8/18/2019 Repetition Cont
3/43
Miguel Music Company’s
ProgramThe sales manager at Miguel’s Music Company
wants a program that allows him to enter the
quarterly sales amount made in each threeregions: Region ! Region "! and Region #$ The
program should calculate the total quarterly
sales and then display the result on the screen$
8/18/2019 Repetition Cont
4/43
Miguel Music Company’s
Program• The program will use a counter to
ensure that the sales manager enterse%actly three sales amounts$
• &t will use an accumulator to total the
sales amount$
8/18/2019 Repetition Cont
5/43
'ample (utput o)
Miguel Music Company’s Program
8/18/2019 Repetition Cont
6/43
'olution to Miguel Music Program
int sales, counter=1, total=0;
while (counter
8/18/2019 Repetition Cont
7/43
8/18/2019 Repetition Cont
8/43
• +se)ul )or counter*controlled loop
• Format:%or( initialization; e&ression ; update )
{1 or ore stateents;
,o -
goes here
Required ;
8/18/2019 Repetition Cont
9/43
8/18/2019 Repetition Cont
10/43
The %or Loop
• 1%pression 2condition3 / any C44
e%pression that is evaluated as )alse or
true$• &) the e%pression is true any statements in
the body o) the for loop are e%ecuted$
• &) the e%pression is )alse the loop will bee%ited! bypassing the body o) the for
loop$
8/18/2019 Repetition Cont
11/43
The %or Loop
• +pdate / represents any C44 statements
that ta0e place a)ter e%ecuting the loop
body$• +sually contains an e%pression that
updates the counter variable
8/18/2019 Repetition Cont
12/43
8/18/2019 Repetition Cont
13/43
for and while loops
%or (int &=1;
&
8/18/2019 Repetition Cont
14/43
The %or Loop
• &n for loop! the variable is o)ten
declared and initiali.ed inside the )or
loop statement$
%or (int &=1; &
8/18/2019 Repetition Cont
15/43
%or Loop 'ample (utput
8/18/2019 Repetition Cont
16/43
'olution to %or Loop
%or (int &=1; &
8/18/2019 Repetition Cont
17/43
8/18/2019 Repetition Cont
18/43
'olution to %or
'quare Loop
• !t is used )or tab 2must be place in coutstatement3
• cout55 6 7t 6
8/18/2019 Repetition Cont
19/43
'olution to %or
'quare Loopcout
8/18/2019 Repetition Cont
20/43
Re"iew o#
Pretest "s$ Posttest Loops• 8hether you use a while loop or for
loop! the loop body might never
e%ecute$• 9oth o) these loops are pretest loops /
loops in which the loop control variable
is tested be)ore the loop body ise%ecuted$
8/18/2019 Repetition Cont
21/43
Pretest "s$ Posttest Loops
• 'ometimes! you want to ensure that a
loop body e%ecutes at least once$
• That way! the code de)initely woulde%ecute one time even i) the loop was
never entered$
• n alternative is to use posttest loop /one in which the loop control variable is
test a)ter the loop body e%ecutes$
8/18/2019 Repetition Cont
22/43
Pretest "s$ Posttest Loops
• There)ore! the instructions in a posttest
loop will always be processed at least
once• 8hile the instructions in a pretest loop
may not be processed i) the condition
initially evaluates to )alse
8/18/2019 Repetition Cont
23/43
The $o4while Loop
• $o4while Loop / controls a loop that
tests the loop*continuing condition at
the end! or bottom! o) the loop$
8/18/2019 Repetition Cont
24/43
• $o4while: a posttest loop 2expression isevaluated a)ter the loop e%ecutes3
• Format:$o
{1 or more statements;
while (expression);
Notice the
required ;
8/18/2019 Repetition Cont
25/43
$o4while ;low o) Control
statement(s)
condition
false
true
8/18/2019 Repetition Cont
26/43
$o4while 'ample (utput
8/18/2019 Repetition Cont
27/43
'olution to $o4while Loop
int &=1;
$o{
cout
8/18/2019 Repetition Cont
28/43
$o4while 'ample (utput
8/18/2019 Repetition Cont
29/43
8/18/2019 Repetition Cont
30/43
8hich o) the )ollowing is )alse=
$ The while loop is a pretest loop$
"$ &n a )or loop! the loop body might nevere%ecute$
#$ The do*while loop controls a loop that
tests the loop*continuing condition at thetop o) the loop$
8/18/2019 Repetition Cont
31/43
nswer
3. The do-while loop controls a loop that
tests the loop-continuing condition at thebottom of the loop.
8/18/2019 Repetition Cont
32/43
8/18/2019 Repetition Cont
33/43
,ested Loop
• loop that completely contains another
is an outer loop (row)$
• loop that )alls entirely within the body
o) another is an inner loop (column)$
• >ou can place one loop 2the inner! or
nested loop3 inside another loop 2the
outer loop3
8/18/2019 Repetition Cont
34/43
for and do-while
'ample (utput
8/18/2019 Repetition Cont
35/43
'olution to %or an$ $o4
while Loop$o{
%or (int &=1; &
8/18/2019 Repetition Cont
36/43
for and for
'ample (utput
8/18/2019 Repetition Cont
37/43
'olution to for an$ for
Loop%or (int row=1; row
8/18/2019 Repetition Cont
38/43
Multiplication Table
'ample (utput
8/18/2019 Repetition Cont
39/43
'olution to
Multiplication Table
cout
8/18/2019 Repetition Cont
40/43
,ested for
'ample (utput
' l ti t
8/18/2019 Repetition Cont
41/43
'olution to
,ested for
%or (int row=1; row
8/18/2019 Repetition Cont
42/43
%an& 'ou
Prepared by: Prof. LRQ Natividad
8/18/2019 Repetition Cont
43/43
%at in all tings!
od may e glori#ied?
Prepared *y: Pro#$ LR+ ,ati"idad