You are on page 1of 43

Repetition Structure


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

Miguel Music Company’s
The sales manager at Miguel’s 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 Company’s
• 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 Company’s Program .

. } cout<<"The total quarterly sales is: " <<total <<endl. while (counter <=3 ) { cout<< "Enter the region " <<counter <<"'s quarterly sales: " . cin>>sa total += sales.Solution to Miguel Music Program int sales. total=0. counter++. counter=1.

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

} No . • Format: for( initialization.• Useful for counter-controlled loop Required . expression . update ) { 1 or more statements. goes here .

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

• If the expression is false the loop will be exited. . • If the expression is true any statements in the body of the for loop are executed.The for Loop • Expression (condition) – any C++ expression that is evaluated as false or true. 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) .

x <= 10 . x++) cout<<x << ". while (x < = 10) { cout<<x << " “ <<endl. x++ } for (int x=1.for and while loops int x=1. .

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

for Loop Sample Output .

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

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 . {for (int y=1. } . y++) cout<< y <<"\t \t" <<y*y <<endl. cout<< "Number \t \tSquare" <<endl . y<=10.

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

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

Posttest Loops • Therefore.Pretest vs. 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 .

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

Notice the required . } while (expression). .• do-while: a posttest loop (expression is evaluated after the loop executes) • Format: do { 1 or more statements.

do-while Flow of Control statement(s) true condition false .

do-while Sample Output .

do{ cout<<x <<" ".Solution to do-while Loop int x=1. x++. }while (x<=10). .

do-while Sample Output .

is useful with counters or if precise number of repetitions is known .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.

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

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

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. do-while). . for.

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

for and do-while Sample Output .

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

for and for Sample Output .

Solution to for and for Loop outer loop for (int row=1. column++) {cout<< "\t" <<row + column <<" ". } } cout<<endl<<endl. inner loop for (int column=1. row <=3 . column<=4. row++) { cout<<endl <<row. .

Multiplication Table Sample Output .

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

Nested for Sample Output .

inner loop for (int column=1. } } .Solution to Nested for outer loop for (int row=1. column++) { cout<< "\t" <<"SBC" <<" ". row++) { cout<< endl <<endl. row<=5. column<=6.

LRQ Natividad .Thank You  Prepared by: Prof.

God may be glorified! Prepared By: Prof.That in all things. LRQ Natividad .