Categories: C++

Loops in C++

In C++ programming language, the repetitive operation is done through loop control instruction. There are three methods by which we can repeat a part of a program.

Types of Loops:
  • Entry Controlled Loops: The test condition is tested before entering the loop body in this type of loop. Entry-controlled loops are For Loop and While Loop.
  • Exit Controlled Loops: The test condition is tested or evaluated at the end of the loop body in this type of loop. As a result, regardless of whether the test condition is true or false, the loop body will execute at least once. The do-while loop is a loop that is exit controlled.
for Loop:

The for loop lets us specify three aspects of a loop in a single line:

  • Initialization: Setting the loop counter to a starting value.
  • Condition: Checking the loop counter to see if it has reached the desired number of repetitions.
  • Updating: Increasing the value of the loop counter after each execution of the program segment within the loop.

Syntax:

for(initialization ; condition ; updation)
{ 
    //statement1
    //statement2
}
Example: Table of 5
#include <iostream>
using namespace std;

int main()
{
 for (int i = 1; i <= 10; i++)
 {
  cout << 5*i<<"\n";
 }

 return 0;
}
Output:
5
10
15
20
25
30
35
40
45
50
Nested for loop:

Just like nested if-else, we can make use of nested for loop as per our requirement.

Syntax:

for(initialization; condition; increment/decrement)
{
    for(initialization; condition; increment/decrement)
    {
        statement;
    }
}
while loop:

In C++, the while loop is an entry-controlled loop (Entry Controlled Loops are used when checking a test condition is mandatory before executing the loop body). It is typically used when the number of iterations is unknown.

Syntax:

initialization;
while(condition)
{
   //code to be executed
   updation;
}
Example:
#include <iostream>
using namespace std;

int main()
{
 // initialization expression
 int i = 1;

 // test expression
 while (i < 6)
 {
  cout <<i <<"\n";

  // update expression
  i++;
 }

 return 0;
}
Output:
1
2
3
4
5
do-while loop:

The do-while loop in C++ is an exit-controlled loop; unlike the for and while loops, the condition is checked at the end of the loop (i.e., after all the statements inside the do-while block have been executed), so the do-while loop runs at least once in the program even if the condition fails.

syntax:

do
{
  //statements to be executed
}while(condition);
Example:
#include <iostream>
using namespace std;

int main()
{
 int i = 2; // Initialization expression

 do
 {
  // loop body
  cout << i<<"\n";

  // update expression
  i=i+2;

 } while (i < 11); // test expression

 return 0;
}
Output:
2
4
6
8
10

Note: also read about the Decision-Making in C++

Follow Me

If you like my post, please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

Share
Published by
Rabecca Fatima

Recent Posts

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

1 day ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

4 days ago

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

3 weeks ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

1 month ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

1 month ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

2 months ago