Vous êtes sur la page 1sur 43

Repetition Structure

(continued)

Counter-Controlled Loops
Loops can be controlled using a
counter rather than a sentinel value
These are called countercontrolled loops

Miguel Music Companys


Program
The sales manager at Miguels Music Company
wants a program that allows him to enter the
quarterly sales amount made in each three
regions: Region 1, Region 2, and Region 3. The
program should calculate the total quarterly
sales and then display the result on the screen.

Miguel Music Companys


Program
The program will use a counter to
ensure that the sales manager enters
exactly three sales amounts.
It will use an accumulator to total the
sales amount.

Sample Output of
Miguel Music Companys Program

Solution to Miguel Music Program


int sales, counter=1, total=0;
while (counter <=3 )
{
cout<< "Enter the region " <<counter <<"'s
quarterly sales: " ;
cin>>sa
total += sales;
counter++;
}
cout<<"The total quarterly sales is: " <<total
<<endl;

The for Loop


for Loop - can be used as an
alternative to the while loop.
It is frequently used in a definite loop or
a loop that must execute a definite
number of times.
It can be used to replace any while
loop.

Useful for counter-controlled loop


Required ;

Format:
for( initialization; expression ; update )

{
1 or more statements;

}
No ;
goes here

The for Loop


Initialization represent any steps you
want to take at the beginning of the
statement.
Most often this includes initializing a
loop control variable.

The for Loop


Expression (condition) any C++
expression that is evaluated as false or
true.
If the expression is true any statements in
the body of the for loop are executed.
If the expression is false the loop will be
exited, bypassing the body of the for
loop.

The for Loop


Update represents any C++ statements
that take place after executing the loop
body.
Usually contains an expression that
updates the counter variable

for Loop Flowchart


initialization
code

update
code

false
test
true
statement(s)

for and while loops


int x=1;
while
(x < = 10)
{
cout<<x <<
" <<endl;
x++
}

for (int x=1;


x <= 10 ; x++)
cout<<x <<
";

The for Loop


In for loop, the variable is often
declared and initialized inside the for
loop statement.
for (int x=1; x <= 10 ; x++)
cout<<x <<
" " ;

for Loop Sample Output

Solution to for Loop


for (int x=1; x<=10; x++)
{
cout<<x <<". "<< "San Beda
College" <<endl ;
}

for Loop Sample Output

Solution to for
Square Loop
\t is used for tab (must be place in cout
statement)
cout<< \t

Solution to for
Square Loop
cout<< "An example of a Number
and its Equivalent Square" <<endl
<<endl ;
cout<< "Number \t \tSquare"
<<endl ;
{for (int y=1; y<=10; y++)
cout<< y <<"\t \t" <<y*y <<endl;
}

Review of
Pretest vs. Posttest Loops
Whether you use a while loop or for
loop, the loop body might never
execute.
Both of these loops are pretest loops
loops in which the loop control variable
is tested before the loop body is
executed.

Pretest vs. Posttest Loops


Sometimes, you want to ensure that a
loop body executes at least once.
That way, the code definitely would
execute one time even if the loop was
never entered.
An alternative is to use posttest loop
one in which the loop control variable is
test after the loop body executes.

Pretest vs. Posttest Loops


Therefore, the instructions in a posttest
loop will always be processed at least
once
While the instructions in a pretest loop
may not be processed if the condition
initially evaluates to false

The do-while Loop


do-while Loop controls a loop that
tests the loop-continuing condition at
the end, or bottom, of the loop.

do-while: a posttest loop (expression is


evaluated after the loop executes)
Format:
do
{
1 or more statements;
} while (expression);

Notice the
required ;

do-while Flow of Control

statement(s)

true
condition

false

do-while Sample Output

Solution to do-while Loop


int x=1;
do{
cout<<x <<" ";
x++;
}while (x<=10);

do-while Sample Output

Deciding Which Loop to Use


while: pretest loop (loop body may not
be executed at all)
do-while: posttest loop (loop body will
always be executed at least once)
for: pretest loop (loop body may not be
executed at all); has initialization and
update code; is useful with counters or if
precise number of repetitions is known

Which of the following is false?


1. The while loop is a pretest loop.
2. In a for loop, the loop body might never
execute.
3. The do-while loop controls a loop that
tests the loop-continuing condition at the
top of the loop.

Answer
3. The do-while loop controls a loop that
tests the loop-continuing condition at the
bottom of the loop.

Nested Loop
A nested loop is a loop inside the body
of another loop
You can place any statements you need
within a loop body.
You can nest any type of loop (while,
for, do-while).

Nested Loop
A loop that completely contains another
is an outer loop (row).
A loop that falls entirely within the body
of another is an inner loop (column).
You can place one loop (the inner, or
nested loop) inside another loop (the
outer loop)

for and do-while


Sample Output

Solution to for and dowhile Loop


do{
for (int x=1; x<=10; x++)
{
cout<<x <<" " ;
}
cout<<endl <<endl;
cout<< "Try again [y/n]?";
cin>>answer;
}while(answer=='y' || answer=='Y');

for and for


Sample Output

Solution to for and for


Loop
outer loop

for (int row=1; row <=3 ; row++)


{
cout<<endl <<row;
inner loop
for (int column=1; column<=4;
column++)
{cout<< "\t" <<row + column <<" ";
}
} cout<<endl<<endl;

Multiplication Table
Sample Output

Solution to
Multiplication Table
cout<< "[5x5] Multiplication Table" <<endl;
for (int outer=1; outer<=5; outer++)
{
cout<< endl <<outer;
for(int inner=2; inner<=5; inner++)
{
cout<< "\t" <<outer * inner <<" ";
}
}

outer loop

inner loop

Nested for
Sample Output

Solution to
Nested for
outer loop

for (int row=1; row<=5; row++)


{
cout<< endl <<endl;
inner loop
for (int column=1; column<=6; column++)
{
cout<< "\t" <<"SBC" <<" ";
}
}

Thank You
Prepared by: Prof. LRQ Natividad

That in all things,


God may be glorified!

Prepared By: Prof. LRQ Natividad

Vous aimerez peut-être aussi