What is a Looping Structure in C++?
A looping structure is a statement used to repeatedly execute a block of one or more statements for a specified number of times or as long as a given condition remains true.
In C++, looping statements are also called Repetitive statements or Iterative Statements.
Different Types of Looping Structures in C++
There are three types of looping structures in C and C++ programming languages, as follows:
Let us study these iterative statements deeply with syntax and purpose with the help of sample codes and outputs.
1. for Loop statement
A for loop statement is used to execute a block of one or more statements repeatedly for a specified number of times.
Syntax of for loop
for(initialization;condition;increment/decrement-expression)
{
statement1;
statement 2;
.
.
.
statement N;
}
Example of for loop
int d;
for(d=5; d>=1; d--)
cout<<d<<endl;
Output:
5
4
3
2
1
2. while Loop Statement in C / C++
while loop is used to execute a block of one or more statements repeatedly as long as the given condition remains true.
Syntax of while loop
while(condition)
{
statement1;
statement 2;
.
.
.
statement N;
}
Example of while loop
int f=5;
while(f>=1)
{
cout<<f<<endl;
f--;
}
Output:
54
3
2
1
3. do while Loop statement in C / C++
A do while loop is used to execute a block of one or more statements as long as the given condition remains true but condition is placed after loop body.
Syntax of do while loop
do
{
statement1;
statement 2;
.
.
.
statement N;
}
while(condition);
Example of do while loop
int h=5;
do
{
cout<<h<<endl;
h--;
}
while(h>=1);
Output:
5
4
3
2
1http://easycppprogramming.blogspot.com/2014/01/working-of-for-loop-statement-with.html
After studying a brief description of Different Types of Iterative Looping Structures in C++
For further reading:
Purpose and Example of do while Loop in CPP Language
Working of While Loop With Examples
0 comments:
Post a Comment
We Love To Hear From You!