Loops In C++:
![]() |
| Loops In C++ |
What is Loop??
Loop in C++ is defined as any statement that runs the specific part of a code or any statement more than one time.
If we look for the types of loops in C++ which can be used we found three types of it.
Types of Loops In C++:
Following are the types of loops:
1. for
loop
2. while
loop
3. do-while
loop
Each one of them has its own syntax, advantages,
disadvantages, and Application.
When we want to run a specific part of a code for more
than one times then Loops are used. It continues to run until some condition is
fulfilled.
If we define a loop we will say that “It is a set of instructions in which we
check the condition for the execution of loop”.
If In any cases when the condition given to the loop is
not fulfilled it return back to the first instruction which is given in the sequence of instructions and it continues to repeat the execution process of the
statements in that specific block.
And If the condition given to the loop has reached, so it
means that the condition has been fulfilled, and control breaks out and comes
out of the Loop and the remaining code is then executed.
For Loop:
For
Loop is used when we know the exact number of iteration or we
can say that it is used when we know how many times a loop is going run.
The code given inside the body of For Loop executes only
if the condition given at the entrance of the loop is fulfilled. It means that
the condition given by us is fulfilled then the code inside the body of the
loop is executed. The loop runs only for the number of times that we have
specified.
Syntax:
The syntax
of the for loop is following:
For (initialization expression; test expression; update
expression)
{
// any statements in the body of the loop;
}
Initialization Expression:
In initialization expression:
We initialize a variable to loop and then we give a value
to it.
Test
Expression:
In test Expression:
we give the condition to the loop then If the condition turns
true, then the code inside of the loop is executed and it updates the variable
we initialized too variable. And if the condition turns false then For loop
will exit.
Update Expression:
In the update expression:
When the condition of the loop turns true than here in
update expression we apply the increment or decrement to the value of the variable.
Example:
A simple example of For
loop is
following:
Source code:
#include <iostream>
using namespace std;
int main()
{
for (int a =0 ; a <= 3; i++)
{
cout << " FOR LOOP:\n";
}
return 0;
}
Output:
FOR LOOP:
FOR LOOP:
FOR LOOP:
While Loop:
Another type of Loop used is While Loop. In while loop, we simply give a condition to the
Loop the loop will continue to execute until the condition remains true.
If the condition given to loop turns wrong in the start
then it will exit and again start the checking process until the condition is
met.
Syntax:
The
syntax of while loop is
following:
While( condition )
{
The statement for the execution;
}
Example:
A simple example for while Loop.
Source code:
#include <iostream>
using namespace std;
int main()
{
Int i=0;
while ( i <7)
{
cout << i<<endl;
i++;
}
Output:
0
1
2
3
4
5
6
Do-While Loop:
In do-while
loop the condition given to the program is checked after the execution
of the program it means that the program at least runs for one time. Then if the
condition is true then further the program executes.
This is the main
difference between while and do-while loop. In a while loop, the condition is
checked first and then the program is executed.
Syntax:
The syntax of While
Loop is the following:
Initialization expression;
do
{
// statements to execute in the body of the loop
update_expression;
}
while (test_expression);
Another difference between the do-while loop and other loops is that:
In do-while
loop, we use a semicolon at the end of the body of the loop, while in other two loops do not use any semicolon
at the end of the body.
Example:
A simple program of the do-while loop.
Source code:
#include <iostream>
using namespace std;
int main()
{
int b = 5; // Here we initialize the expression
do
{
cout << " do while loop\n";
b++; // Here we are incrementing/updating the expression
}
while (i < 1); //
here we are testing the expression
return 0;
}
Output:
Do while loop

No comments:
Post a Comment