0% found this document useful (0 votes)
6 views

Topic 4 - Repetition Control Structure (Part 1)

The document discusses repetition control structures in programming, specifically covering for loops and nested for loops. It defines loop control variables and explains how for loops work by initializing a variable, evaluating a condition, and updating the variable. Examples are provided to demonstrate basic for loops and nested for loops.

Uploaded by

Aqilah Najwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Topic 4 - Repetition Control Structure (Part 1)

The document discusses repetition control structures in programming, specifically covering for loops and nested for loops. It defines loop control variables and explains how for loops work by initializing a variable, evaluating a condition, and updating the variable. Examples are provided to demonstrate basic for loops and nested for loops.

Uploaded by

Aqilah Najwa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

TOPIC 4:

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:

for(initial statement; loop condition; update statement)


{
statement1;
statement2;
statement3;
}
THE for LOOP STATEMENT (cont.)
3. The flowchart:
THE for LOOP STATEMENT (cont.)
4. The for loop executes as follows:
• Step 1: The initial statement executes
• Step 2: The loop condition is evaluated. If the loop condition evaluates to
true:
✓ Execute the for loop statement
✓ Execute the update statement (the third expression in the parentheses)
• Step 3: Repeat Step 2 until the loop condition evaluates to false
THE for LOOP STATEMENT (cont.)
5. Example: A loop to print "Hello World!" three times

Output:
1 2 4
for (int i=1; i<=3; i++)

3 cout << i <<") Hello World!" << endl;

i i<=3 Loop statement i++


i=1 1<=3 1) Hello World! i=1+1
(True)
i=2 2<=3 1) Hello World! i=2+1
(True) 2) Hello World!
i=3 3<=3 1) Hello World! i=3+1
(True) 2) Hello World!
3) Hello World!
i=4 4<=3 - -
(False)
THE for LOOP STATEMENT (cont.)
6. Variety of for loop statements:

for loop statement Output Description


The for loop runs three times.
Within the loop, the cout
statement prints "Hello World" and
another cout statement prints a
star symbol. This process repeats
until the loop completes three
iterations.

The given code is a C++ program


that uses a for loop to print "Hello
World" and a star symbol three
times. However, the code is not
correctly formatted, and the
second cout statement is not part
of the for loop.
THE for LOOP STATEMENT (cont.)
6. Variety of for loop statements (cont.):

for loop statement Output Description


In this code, the for loop runs three
times, with the loop variable i starting
at 3 and decrementing by 1 each time
until it reaches 1. Within the loop, the
cout statement prints the value of i
and a space character. After the loop
completes, another cout statement
prints a line of five stars.

In this code, the for loop runs five


times, with the loop variable i
starting at 1 and incrementing by 2
each time until it reaches 10. Within
the loop, the cout statement prints
the value of i with spaces in between.
THE for LOOP STATEMENT (cont.)
6. Variety of for loop statements (cont.):

for loop statement Output Description


The issue is due to the semicolon (;)
immediately after the for loop declaration.
The semicolon effectively terminates the for
loop, making the subsequent cout statement
not part of the loop.

Due to the semicolon immediately after for


loop, the loop body is empty. The loop will
iterate three times, incrementing i each time,
but it won't execute any statements within
the loop because of the semicolon.

The cout statement is not part of the loop


because of the semicolon. It will be executed
once after the loop has completed, and it will
print the final value of i, which is 4
EXERCISE
Write a C++ for loop program to:
a) Display all the numbers from 1 to 20.
b) Display all the even numbers from 10 to 50.
c) To ask the user to input 5 numbers.
THE nested for LOOP STATEMENT
1. A nested loop is a loop inside of another loop
2. The nested loop is known as the inner loop and the loop in which is
nested is known as the outer loop

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.

for(int i=1; i<=4; i++)


{
for(int x=4; x>=i; x--)
cout<<"$";

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

3. These three types of loop require a loop control variable (LCV) to


control the loop
SUMMARY (cont.)
4. The for loop is a control flow statement that allows you to
repeatedly execute a block of statements based on a specified
condition.

5. The for loop initializes a control variable at the beginning, checks


a condition before each iteration, and updates the control
variable after each iteration.

6. The nested loop is a loop structure within another loop. It allows


for the execution of one or more loops inside the body of another
loop
THANK YOU
FOR YOUR ATTENTION

TO BE CONTINUED IN PART 2…

You might also like