March 19, 2024

Loops in C++, 3 Types of loops & their syntax

Hey guys, what is up. In this particular post, we will be talking about loops in c++. Types of loops in c++ and their syntax and will be seeing 1-1 example of each type. We will also be talking about the difference between types.

Updated on 29/9/2021

KEY POINTS

Loops in c++:

Loops in c++ are also known as repetition control structures or grouping structures. As they repeat a set of statements in a program, till a given condition is true. Loops are of 3 types.

Types of loops:

  • For loop
  • While loop
  • Do-while loop

For loops in c++:

For loop is used when the loop body is to execute for a fixed number of times. It is an entry control loop.

Syntax: 

for(initialization; condition; increment/decrement)

{

    ……. (statements block);

}

Execution of for loop: 

The initialization expression is executed firstly, the test expression is evaluated if it is true. Then the body containing a set of statements is executed and then control moves to increment and decrement expression. Otherwise, the loop terminates the body of the loop. Again after executing increment and decrement expression, the condition is checked. This process repeats till the condition remains true.

Example:

Ques: To print counting from 1 to 10 using for loop.

#include <iostream.h>

#include<conio.h>

void main()

{

          clrscr();

          int n;

          for(n=1; n<=10; n++)

          {

                   cout<<n<<“\n”;

          }

          getch();

}

Output:

output for loops in c++

While loops in c++:

A while loop is a pre-test loop. It first tests the specified conditional expression and as long as the condition expression is true, statements are executed. While loop is also called the entry check loop. This loop is used when we are not sure of the number of times the loops will be executed.

Syntax:

while(condition)

{

….. (statements block);

Increment/decrement;

}

Example:

Ques: To print numbers from 0 to 4 using while loop.

#include <iostream.h>

#include<conio.h>

void main()

{

              clrscr();

              int n=0;

              while(n<5)

              {

                        cout<<n<<“\n”;

                        n++;

              }

              getch();

}

Output:

output while loop

Do while loops in c++:

 Do while is a post-test loop. It is similar to a while loop except it executes its body at least once whether the condition is true or false in the beginning. It is an exit control loop i.e. condition is checked after execution. 

Syntax:

do

{

….. (statements block)

Increment/decrement;

}while(condition);

Example:

Ques: To print statement Hello everyone 11 times using do-while loop.

#include <iostream.h>

#include<conio.h>

void main()

{

              clrscr();

              int n=0;

              do

              {

                        cout<<“Hello Everyone”<<“\n”;

                        n++;

              }while(n<=10);

              getch();

}

Output:

output do while loops in c++

Difference between loops:

FORDO-WHILE
It is an entry control loop.It is an exit control loop
The body of the loop is not performed even once if the condition is false.The body of the loop is performed at least one time.
Their are increment, decrement, and initialization in the syntax. Their is no increment, decrement, and initialization in the do-while loop.
DO-WHILEWHILE
It is an exit control loop.It is an entry control loop.
The body of the loop is carried out at least one time.The body of the loop is not performed even once if the condition is false.

In the upcoming post, you will be getting information related to one of the trending technology topics and Operators in coding.  If you haven’t read about what is switch statement in c++, then go and read this. You can also read previous posts by scrolling downwards. If you want more about any one of the technology topics  then go and comment on the same in the comment section. Also, stay tuned for the upcoming post.

Leave a Reply

Your email address will not be published. Required fields are marked *