Types and uses of Loops in C++

Types and uses of Loops in C++
Techiio-author
Written by Sagar RabidasFebruary 9, 2022
9 min read
C++
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Techiio-author
Sagar Rabidas

Software Developer

In this blog, we will discuss the Types and uses of Loops in C++

Loops in C++

Loop statements in c++ execute a certain block of the code or assertion a couple of times, specifically used to lessen the period of the code using executing the equal function a couple of times, lowering the redundancy of the code. C++ helps various sorts of loops like for loop, whilst loop, do-whilst loop, everyone has its syntax, benefits, and usage. In the programming global, the loop is a control shape this is used when we need to execute a block of code, a couple of times. It normally maintains to run till and unless some give up situation is fulfilled.

If we did not have loops, we would have to use the iterative method to print a repetitive block of statements, which would look something like this:

#include <iostream>
using namespace std;
int main()
{
cout << " Good morning \n";
cout << " Good morning \n";
cout << " Good morning \n";
cout << " Good morning \n";
cout << " Good morning \n";
}

Types of Loops in C++

Now that we have seen how a Loop works, let us make it clearer by going through the types of Loops out there. In C++ programming, we have three types of Loops in C++ :

  • For Loop
  • While Loop
  • Do While Loop

For Loop

Loop is an entry-controlled loop, meaning that the condition specified by us is verified before entering the loop block. It is a repetition control structure. The loop written by us is run a specified number of times.

To control the loop, we use a loop variable in For loop. This variable is first initialized to some value, then we perform the check on this variable comparing it to the counter variable, and finally, we update the loop variable.

Syntax:

for(initialization expression; test expression; update expression)
{
// statements to execute in the loop body
}

Initialization Expression:

Here we initialize the loop variable to a particular value. For example, int i=1;

Test Expression:

Here, we write the test condition. If the condition is met and returns true, we execute the body of the loop and update the loop variable. Otherwise, we exit the For loop. An example for test expression is i <= 5;

Update Expression:

Once the body of the loop has been executed, we increment or decrement the value of the loop variable in the update expression. For example, i++;

Let us look at an example of For loop:

#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; i++)
{
cout << " Good morning \n";
}
return 0;
}

While Loop

While loop is also an entry controlled loop, where we verify the condition specified by us, before running the loop. The difference is being that we use For loops when we know the number of times the body of the loop needs to run, whereas we use while loops in circumstances when beforehand we do not know the precise number of times the body of the loop needs to run. The execution of the loop is terminated based on the test condition.

Syntax:

initialization expression;

while (test_expression)
{
// statements to execute in the loop body
update_expression;
}

The syntax of the loops differs only in the placement of the three expression statements.

Let us look at an example of a while loop:

#include <iostream>
using namespace std;
int main()
{
int i = 0;    // initialization expression
while (i < 5)   // test expression
{
cout << "Good morning\n";
i++;    // update expression
}
return 0;
}

Do While Loop

Do while loop is an exit controlled loop, meaning the test condition is verified after the execution of the loop, at the end of the body of the loop. Hence, the body executes at least once, regardless of the result of the test condition, whether it is true or false. This happens to be the foremost difference between while loop and do while. In a while loop, the condition is tested beforehand, whereas in a do-while loop the condition is verified at the finish of the body of the loop.

Syntax:

initialization expression;
do
{
// statements to execute in the loop body
update_expression;
} while (test_expression);
In do while loop, we end the body of the loop with a semicolon, whereas the other two loops do not have any semicolon to end the body of their loops.
#include <iostream>
using namespace std;
int main()
{
int i = 2;    // initialization expression
do
{
cout << " Good morning\n";
i++;       // update expression
} while (i < 1);  // test expression
return 0;
}

An infinite loop or an endless loop is a loop that does not have a proper exit condition for the loop, making it run infinitely. This happens when the test condition is not written properly and it permanently evaluates to true. This is usually an error in the program.

#include <iostream>
using namespace std;
int main ()
{
int i;
for ( ; ; )
{
cout << "This loop runs indefinitely.\n";
}
}

C
C++
Loops in C++
Do While loop
For loop
1 VIEWS 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance