Unit-V.-Repetition-Control-Structure2-1
Unit-V.-Repetition-Control-Structure2-1
Loops(Part 2)
Leonylyn P. Bensi
CICT Faculty
Topics
Types of loops.
The concept of for loop, while loop, do while loop
and an update loop using prompt.
How to make a program using the different types
of looping,
Learning objectives:
Forloop
While loop
Do while loop
Nested loop
For Loop
A for loop is a repetition control structure that allows you
to efficiently write a loop that needs to execute a specific
number of times.
The syntax of a loop in c++ is:
initialization;
while (comparison expression)
{
statement/s;
loop update;
}
EXAMPLE:
control variable
cout<<ctr<<endl;
loop body
ctr++;
loop update
Flow diagram of while loop
Sample program using while loop
LOOP UPDATE USING A PROMPT
The loop update will be a question prompting the
user if he wants to continue or stop executing the
loop.
The only difference is that the comparison happens at the end of the
do while loop so the body of the loop is executed at least once even
if the comparison expression evaluates to false, unlike in the for loop
and the while loop where the comparison expression happens before
the loop body.
This is the reason why, in the for and while loops, when the
comparison evaluates to false, the loop body will not be executed
even once.
Syntax of Do while Loop
initialization;
do
{
statement/s;
loop update;
}
while (comparison expression);
Sample Program for do while loop
Laboratory Activity 4, 5, & 6
Problem Statement:
Write a C++ program that prompts users to enter their name and
input grades for 5 subjects. The program should display the
student's name, the grades, and the average grade. Implement this
using three different loops: for, while, and do-while. Each program
version should follow the same basic structure: input the student's
name, input the 5 grades, then output the name, and the average
grade.
NESTED LOOPS
A nested loop is a loop within a loop. It is consisting of an outer loop and an
inner loop.
The outer loop will be executed first. When the evaluation of the comparison
operators evaluates to true, the loop body of the outer loop will be executed.
Inside the body of the outer loop, the inner loop is embedded. The inner loop
executes and when the comparison of the inner loop evaluates to true, the loop
body of the inner loop will be executed repeatedly until such time that the
comparison expression results to false.
The inner loop terminates at this point and the rest of the statements inside the
loop body of the outer loop will be executed.
Then, the update of the outer loop will be performed and the process will be
repeated until the outer loop terminates.
Example of Nested Loop
Note: The number of times the nested loop will execute is equal to the number
of times the outer loop executes and the number of times the inner loop
executes.
Every after condition inside the inner loop, the loopcounter will increment by 1.
Also, the value of x will increment by 1 and compare it to outer loop condition. When the outer condition
evaluates to True. The inner loop will evaluate again as long as the outer loop satisfied the condition.
Then, the statement outside the outer loop will be executed.
Sample Program output for nested loop
Sample Program for nested loop
Sample Program for nested loop
Now! It’s Your turn
Write a program that will print the
sample output below
Answer
Give the output
Output
Thank you !
Any question(s)?
ASSIGNMENT. Write your answer in your CC
101 Notebook
1. Create a C++ program that will print the output below:
Assignment
2. Create a c++ program that will print the multiplication table from 1 to 5.
Please refer the output below: