Topic 4 - Repetition Control Structure (Part 1)
Topic 4 - Repetition Control Structure (Part 1)
REPETITION
CONTROL STRUCTURE
(PART 1)
OBJECTIVES
In this chapter, you will:
✓ Differentiate between the three types of repetition control
structure
✓ Explore the syntax and develop for loop structure to
iterate over a range of values
✓ Understand the concept of a nested loop
✓ To evaluate nested loop problems
INTRODUCTION
1. Repetition structure: Direct the computer to repeat one or more instructions until
some condition is met
✓ Also called a loop or iteration
2. Examples of problems requiring repetition structure:
✓ User authentication: Employing a loop for the login system (password
verification)
✓ Automated email distribution: To automate the process of sending emails to
multiple recipients
✓ Social media updates: To add and manage new posts on platforms such as
Twitter efficiently
INTRODUCTION (cont.)
3. Three basic types of repetition structure:
✓ for loop
➢ nested for loop
✓ while loop
➢ counter-controlled while loop
➢ sentinel while loop
✓ do…while loop
Sequence control structure
WHY IS REPETITION
NEEDED?
1. For example, to add five numbers:
✓ Declare a variable for each
number, input the numbers,
and add the variables together
WHY IS REPETITION NEEDED? (cont.)
✓ Create a loop that reads a
Repetition control structure
number into a variable
and adds it to a variable
that contains the sum of
the numbers
LOOP CONTROL VARIABLE (LCV)
1. The Loop Control Variable (LCV) is a variable that is used to control the flow of
a program
2. It is used to determine when to exit a series of instructions to make sure that
the program will not be stuck in an infinite loop.
3. The LCV must be tested either before or after processing any instructions to
ensure that the program runs smoothly
4. The LCV consists of three processes which are initialization, evaluation and
update statement
LOOP CONTROL VARIABLE (LCV)-cont.
5. These processes must be
controlled by using only one
Loop control variable (LCV)
variable to make sure that no
runtime error happens during
the compilation process
THE for LOOP STATEMENT
1. The for loop is an entry control loop when an action is to be repeated for a
predetermined number of times
2. The general syntax of for loop:
Output:
1 2 4
for (int i=1; i<=3; i++)
Outer loop
Inner loop
THE nested for LOOP STATEMENT(cont.)
3. Example:
Output:
i i<=2 Outer loop statement i++
1 1<=2 1) i is now 1 i=1+1
(True)
2) j j<=3 Inner loop statement j++
1 1<=3 i is now 1 j=1+1
(true) j is now 1
2 2<=3 i is now 1 j=2+1
(true) j is now 1
j is now 2
3 3<=3 i is now 1 j=3+1
(true) j is now 1
j is now 2
j is now 3
4 4<=3 - -
(false)
3 i is now 1
j is now 1
j is now 2
j is now 3
*blank space line*
i i<=2 Outer loop statement i++
2 2<=2 1) i is now 1
(True) j is now 1
j is now 2
j is now 3
i is now 2
2) j j<=3 Inner loop statement j++
1 1<=3 i is now 1 j=1+1
(true) j is now 1
j is now 2
j is now 3
i is now 2
j is now 1
2 2<=3 i is now 1 j=2+1
(true) j is now 1
j is now 2
j is now 3
i is now 2
j is now 1
j is now 2
i i<=2 Outer loop statement i++
3 3<=3 i is now 1 j=3+1 i=2+1
(true) j is now 1
j is now 2
j is now 3
i is now 2
j is now 1
j is now 2
j is now 3
4 4<=3 - -
(false)
3) i is now 1
j is now 1
j is now 2
j is now 3
i is now 2
j is now 1
j is now 2
j is now 3
*blank space line*
3 3<=2 - -
(false)
THE nested for LOOP STATEMENT(cont.)
4. Example: Write a program to display the following output.
THE nested for LOOP STATEMENT(cont.)
5. Example: Write a program to display the following output.
EXERCISE
Trace the output of the following program segment.
cout<<endl;
}
SUMMARY
1. The repetition/looping control structure is important to repeat the
same group of statements
2. There are three looping control structures in C++—the for loop, the
while loop, and the do. . .while loop
TO BE CONTINUED IN PART 2…