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

Unit-V.-Repetition-Control-Structure2-1

This document covers the concept of loops in programming, specifically focusing on for loops, while loops, do while loops, and nested loops. It explains their syntax, flow control, and provides sample programs for each type of loop. Additionally, it includes a laboratory activity that requires writing a C++ program to input grades using different looping methods.

Uploaded by

aricketchup
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Unit-V.-Repetition-Control-Structure2-1

This document covers the concept of loops in programming, specifically focusing on for loops, while loops, do while loops, and nested loops. It explains their syntax, flow control, and provides sample programs for each type of loop. Additionally, it includes a laboratory activity that requires writing a C++ program to input grades using different looping methods.

Uploaded by

aricketchup
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Unit I.

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:

At the end of this lesson, you will learn:


 Different types of loops and their definition;
 Understand the concept of the different types of loops;
 Make a program using the different types of loops.
Types of Loops

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:

for (initialization; comparison expression; update)


{
statements;
}
Three flow of controls in for loop

 The initialization of the loop control


variable
 The comparison expression
 The body of the for loop
The flow diagram of the for loop
Sample program for for loop
while loop

 It will also do repetitive tasks assigned to it just like in a


for loop.

 In a while loop, the initialization of the loop control


variable is typed before the structure of the loop. The
loop update on the other hand is placed inside the loop
body.
Syntax of while loop.

initialization;
while (comparison expression)
{
statement/s;
loop update;
}
EXAMPLE:

initialization of the loop

control variable

comparison of the loop

control variable to the


ctr = 1 ;
sentinel value
while ( ctr <= 5 )

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.

 Every time the user enters a positive answer to the


question, the loop body will be executed.

 It will stop the execution of the loop body when it


receives a negative response to the question.
Sample program using loop update with a prompt
THE do while LOOP
 The do while loop functions just like a for loop or a while 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.

totalLoop = outerloop * innerloop;


Sample Program for nested loop
Simulation
Integer variables : x, y, and loopcounter = 0

outer for loop (for x=1;x<=3;x++)


X condition ans(True/False)
1 1<=3 True -- If the answer in the condition is true it will proceed to the inner loop
2 2<=3 True --If the answer in the condition is true it will proceed to the inner loop
3 3<=3 True --If the answer in the condition is true it will proceed to the inner loop
4 4<=3 False-- Loopcounter value
1
Inner loop (for y=1; y<4;y++)
2
y condition True/False Print 3
1 1<=4 True 1_ 4
5
2 2<=4 True 2_ 6
3 3<=4 True 3_ 7
8
4 4<=4 False
9

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:

You might also like