Computer Programming (M6-Main) PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

COMPUTER

PROGRAMMING 1
MODULE 6
REPETITION CONTROL
STRUCTURES
Upon completion of this module, you will be able to:
• Familiarize the different repetition control structure
• Discuss the different ways on how the loop is
controlled
• Simulate a loop statement to determine the output
• Repetition structures, or loops, are used when a
program needs to repeatedly process one or more
instructions until some condition is met, at which time
the loop ends.
• The process of performing the same task over and
over again is called iteration, and C++ provides built-
in iteration functionality.
C++ provides three different forms of repetition
statements:
• while structure
• for structure
• do-while structure
The condition being tested can be evaluated at either
(1) the beginning or (2) the end of the repeating
section of code.

If the test occurs at the beginning of the loop, the


type of loop is called a pre-test loop or entrance-
controlled loop.

If the test occurs at the end of the loop, the type of


loop is called a post-test loop or exit-controlled-loop.
Counter-Controlled Repetition requires:
• the name of a control variable (or loop counter)
• the initial value of the control variable
• the loop-continuation condition that tests for the
final value of the control variable to determine
when to exit
• the control variable to be incremented (or
decremented) each time through the loop
The for statement is used for repeating a statement
or series of statements as long as a given conditional
expression evaluates to true.
• condition is tested first (pre-test loop)

Syntax:
for (initialization; condition; update statement){
statement(s);
}
START

num = 1
Write a program
false
that prints the
num <= 3
numbers 1-3 in
true
horizontal
manner. Display num statement

OUTPUT:
num=num+1

END
Consider the block of code below:

for(num=1;num<=3;num++)
cout<<num<<" ";

Let us simulate the content of the variable num and the


screen output.
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 1

Output:
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 1 Is num=1 <= 3? YES, go to statement

Output:
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 1 Then proceed to update statement num++

Output: 1_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 2 num is incremented by 1 (because of ++),


then proceed to condition

Output: 1_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 2 Is num=2 <= 3? YES, go to statement

Output: 1_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 2 Then proceed to update statement num++

Output: 1_2_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 3 num is again incremented by 1 (because of


++), then proceed to condition

Output: 1_2_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 3 Is num=3 <= 3? YES, go to statement

Output: 1_2_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 3 Then proceed to update statement num++

Output: 1_2_3_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 4 num is again incremented by 1 (because of


++), then proceed to condition

Output: 1_2_3_
for(num=1;num<=3;num++)
cout<<num<<" ";

num: 4 Is num=4 <= 3? NO, exit for statement then


proceed to next statements outside for

Output: 1_2_3_
WHAT IF num<=3 BECOMES num>=3?
WHAT HAPPENS TO THE OUTPUT?
UNDERSTANDING
LOOPING STRUCTURE
WHAT IF num++ BECOMES num--?
WHAT HAPPENS TO THE OUTPUT?
UNDERSTANDING
LOOPING STRUCTURE
START

counter = 1, sum = 0, num

Write a program false


counter <= 3
that reads three
integers and true

displays the sum Input num

of all the integers.


sum = sum + num
OUTPUT:
counter++

Display sum

END
The while statement is used for repeating a statement or
series of statements as long as a given conditional
expression is evaluated to true.
• condition is tested first (pre-test loop)

Syntax:
while( condition expression){
statement(s);
}
Write a program
that reads an
integer that Given the
determines how code at the
many asterisks left, what it
will be displayed seems to be
on screen in the problem?
horizontal
manner.
Remember in Counter-Controlled Repetition, it requires
• the name of a control variable ✓ asterisk
• the initial value of the control variable 
• the loop-continuation ✓ asterisk<=n
• the control variable to be incremented
or decremented 
HOW ARE WE GOING TO CORRECT
THE CODE?
UNDERSTANDING
LOOPING STRUCTURE
START

n, asterisk = 1

Input n
false

asterisk <= n
OUTPUT: true

Display “*”

asterisk++

END
Write a program
which is a custom
countdown from
an inputted
number down to
1, and displays OUTPUT:
the word, “FIRE!”
WHAT IF n>0 BECOMES n<0? WHAT
HAPPENS TO THE OUTPUT?
UNDERSTANDING
LOOPING STRUCTURE
WHAT IF --n BECOMES ++n? WHAT
HAPPENS TO THE OUTPUT?
UNDERSTANDING
LOOPING STRUCTURE
Sentinel variable tested in condition; loop ends when
sentinel encountered.
Syntax:
cin >> variable; //initialize loop control variable
while (variable != sentinel) {
.
.
cin >> variable; //update loop control variable
.
}
START

sum=0

Write a program that Input x


accepts an input false
continuously as long x!=99
as the input is not 99. If true

the input is 99, the sum = sum + x


program must compute OUTPUT:
the sum of the inputs Input x
except for the sentinel
99 then exits. Print sum

END
The do..while statement executes a statement or
statements then repeats the execution as long as a given
conditional expression evaluates to true.
• Statements in the loop are executed first at least once,
and condition is tested last (post-test loop)
Syntax:
do {
statements;
} while (conditional expression);
Write a program that
asks for a number
and displays it. The
program only
terminates when the
OUTPUT:
input zero(0).
Write a program
that displays the
number between
the starting value
and ending
value(inclusive).
OUTPUT:
The continue statement is used inside loops. When a
continue statement is encountered inside a loop,
control jumps to the beginning of the loop for next
iteration, skipping the execution of statements inside
the body of loop for the current iteration.
OUTPUT:

Write a program that


prints on screen the
numbers 0 to 8
except 4 in horizontal
manner.
It is used to come out of the loop instantly. When a
break statement is encountered inside a loop, the
control directly comes out of loop and the loop gets
terminated. It is used with if statement, whenever
used inside loop.
Write a program that
displays the value of
the variable from 1 to
50 but exits right away
when the value
reaches 3.

OUTPUT:
It allows making an absolute jump to another point in the
program. You should use this feature carefully since its
execution ignores any type of nesting limitation.

The destination point is identified by a label, which is


then used as an argument for the goto instruction.

A label is made of a valid identifier followed by a colon


(:). i.e. Here:
Write a program using
goto statement that label
counts down from 10 to
1 then prints “FIRE!”

OUTPUT:
Consider the block of code below:
initial relational terminal
Value(IV) operator Value(TV)

for(num=1;num<=3;num++)
cout<<num<<" ";
Given that the increment/decrement operator is ++/--
respectively and the first testing is true.
no. of loops = |TV – IV| + 1 relational operator is <=,>=
no. of loops = |TV – IV| relational operator is <,>
for(num=1;num<=3;num++)
cout<<num<<" ";
IV = 1
TV = 3
Relational operator: <=
Formula: |TV – IV|+1
No. of loops = |3 – 1| + 1
No. of loops = 3
3. for(n=1;n<=10;n++)
cout<<n<<" ";

4. n=7;
Identify the no of loops given the do{
block of code. cout<<n<<" ";
n--;
1. for(n=0;n<3;n++) }
cout<<n<<" "; while(n>=1);

2. n=5 5. n=1
while(n>1){ while(n>1){
cout<<n<<" "; cout<<n<<" ";
n--; n--;
} }
n=5
while(n>1){
cout<<n<<" ";
n++;
}
HOW MANY TIMES DOES IT LOOP?

UNDERSTANDING
LOOPING STRUCTURE
A loop becomes infinite loop if a condition never
becomes false.

In the given example, it is an infinite loop because as


we are incrementing the value of n so it would always
satisfy the condition n++, the condition would never
return false.
A loop inside another loop is called a nested loop. The
number of loops depend on the complexity of a problem.

Suppose, a loop, outer loop, running n number of times


consists of another loop inside it, inner loop,
running m number of times. Then, for each execution of
the outer loop from 1...n, the inner loop runs maximum
of m times.
OUTPUT:

In this program, the


outermost loop runs 5
times and for every
loop, the innermost loop
runs i times which is 1 at
first, meaning only "1" is OUTER LOOP
printed, then on the next
loop it's 2 numbers INNER LOOP
printing "1 2" and so on
till 5 iterations of the
loop executes, printing
"1 2 3 4 5". This way, the
given number pattern is
printed.
OUTPUT:
Modify the program about the multiplication table to
produce the output below.
OUTPUT:
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition). Sams Publishing
• McGrath, M. (2017). C++ programming in easy steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
• Tale, T. (2016). C++: The Ultimate Beginners Guide to C++ Programing.
CreateSpace Independent Publishing Platform
• Cadenhead, R et. Al. (2016). C++ in 24 Hours, Sams Teach Yourself (6th
Edition).Sams Publishing
• McGrath, M. (2017). C++ Programming in Easy Steps (5th ed.).
Warwickshire, United Kingdom: Easy Steps Limited
• Deitel (2017), How to Program C++, 10th Edition

You might also like