Professional Documents
Culture Documents
Computer Programming (M6-Main) PDF
Computer Programming (M6-Main) PDF
Computer Programming (M6-Main) PDF
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.
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<<" ";
num: 1
Output:
for(num=1;num<=3;num++)
cout<<num<<" ";
Output:
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_2_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_2_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_2_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_2_3_
for(num=1;num<=3;num++)
cout<<num<<" ";
Output: 1_2_3_
for(num=1;num<=3;num++)
cout<<num<<" ";
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
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
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:
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.
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.