Loops in C++: For Loops, While Loops, and Do-While Loops

Introduction to Loops in C++ Programming Language.

A loop statement executes a group of statements repeatedly until a condition is met. There are 3 types of primitive loops in C ++: for, while and do-while. In this article, I will introduce you to loops in C ++ programming language and its types.

Introduction to Loops in C ++

Loops in the C ++ programming language are used to execute a block of statements repeatedly until a particular condition is met. A loop consists of an initialize instruction, a test condition, and increment instruction.

Also, Read – What is Code Review and How to do a Code Review?

There are three types of loops in the C ++ programming language:

Loops in C++

For Loops:

A for loop executes instructions in the body of the loop, while the condition of the loop is true. Before the loop initialization instruction is executed once. After each cycle, the iteration execution part is executed.

A for loop is defined as follows:

#include<iostream>
using namespace std;
int main(){
    for (int i=1;i<=5;i++){
        cout<<i<<endl;
    }
    return 0;
}
1
2
3
4
5

The for loop is initialized by the value 1, the test condition is i <= 5 i.e. the loop is executed until the value of i remains less than or equal to 5. At each iteration, the value of i is incremented by one by doing i ++.

While Loops:

A while loop executes statements repeatedly until the given condition evaluates to false. This control instruction is used when it is not known in advance how many times a block of code should be executed.

A while loop is defined as follows:

#include<iostream>
using namespace std;
int main(){
    int i=1;
    while (i<=5){
        cout<<i<<endl;
        i++;
    }
    return 0;
}
1
2
3
4
5

The while loop is initialized by the value 1, the test condition is i <= 5 i.e. the loop is executed until the value of i remains less than or equal to 5. At each iteration, the value of i is incremented by one by doing i ++.

Do-while Loops:

A do-while loop is very similar to a while loop, except that the condition is checked at the end of each cycle, not at the beginning. The loop is therefore guaranteed to run at least once.

A Do-while loop is defined as follows:

#include<iostream>
using namespace std;
int main(){
    int i = 1;
    do
    {
        cout<<i<<endl;
        i++;
    } while (i<=5);
    return 0;
}
1
2
3
4
5

The do-while loop variable is initialized by the value 1, at each iteration, the value of i is incremented by one by doing i ++, the test condition is i <= 5 that is to say that the loop is executed until the value of i remains less than or equal to 5. Since the test condition is only true after the loop has already been executed, a do-while loop is executed at least once.

Also, Read – If Else Statements in C++ Programming Language.

Hope you liked this article on loops in C ++ programming language. Please feel free to ask your valuable questions in the comments section below.

Default image
Aman Kharwal

I am a programmer from India, and I am here to guide you with Data Science, Machine Learning, Python, and C++ for free. I hope you will learn a lot in your journey towards Coding, Machine Learning and Artificial Intelligence with me.

Leave a Reply