C++
LOOPS
Topics to be discussed
• Topics to be discussed
loops
For loop
While loop
Do while loop
Nested loop
Break
continue
References and activities
• References
Dietel & Dietel, How to program chapter # 2
Robert Lafore, Object Oriented Programming chapter #3
Loops
• A type of control structure that repeats a statement or set statements is known as looping structure.
• Loops are control structures used to repeat a given section of code a certain number of times or until a
particular condition is met.
• Loops are also called iterative or repetitive structure.
• In sequential structure, all statements are executed once. On the other hand, conditional structure may
execute or skip a statement on the basis of some given condition.
• In some situation , it is required to repeat a statement or number of statements for a specified number
of times.
• Loop structures are used for this purpose
Continue…
Loops are basically used for two purposes:
• To execute a statement or number of statements for a specified number of times.
For example, a user may display his name on screen for 10 times.
• To use a sequence of values. For example, a user may display a set of natural
numbers from 1 to 10.
• There are three types of loops in C++.
a. While loop b. Do-While loop c. For loop
Types of Loops
• Types of Loops
There are three types of loops used in C++. These are as under:
1. for-loop
2. While-loop
3. Do-while-loop
1. for-loop
• for loop
for loop executes one or more statements for a specified number of times
It is that type of loop which we use when we know in advance that how many
times the body of the loop will be executed.
This loop is also called counter-controlled loop
It is the most flexible form of loop
That is why, it is the most widely used loop by the programmers
for loop syntax
• Syntax of for loop
• The general syntax of for-loop is as under:
for(initialization; testing; increment / decrement)
statement;
• Syntax of for loop for more than one statement
for(initialization; condition; increment / decrement)
{
statement 1;
statement 2;
statement N;
}
Continue…
for(x=1; x<=10; x++)
Initialization
Increment/Decrement
Condition
for loop flow chart
A program to print a message 10 times
on the screen using for loop.
C++ program to print natural numbers
from 1 to 10 using for loop.
C++ program to print both numbers and
a message several times using for loop.
C++ program to print numbers in reverse
order from 10 to 1 using for loop
C++ program to print the table of 2 ,
using for loop.
C++ program to print the table of any
number using for loop.
C++ program to print the table of any
number and length using for loop.
C++ program to print the summation of
1 to 10 numbers using for loop.
C++ program to ask the user for entry of
numbers and display the addition of
numbers. The user enter numbers more than
once using for loop.
Thank You