Cpp while loop - looping C++ Programming

Hello Guys, Today, we will teach you C++ While LoopLooping C++ Programming, loops are using to execute a-

-block of code as long as well as a specified condition is reaching.

Mostly we are using repeatedly one block of code in the same program. but it wastes time and makes mistakes.

Loops are the most useful because it saves your time, and reduce the error chance and they boost the readability in code.

C++ While Loop

The while loop is looping through a block of code as well as a specific condition is true. the syntax of while loop is:

while(condition)
{
    // block of code to be executed
}

I hope you see the syntax of the while loop carefully. So, In the example below.

we try to understand you while loop via complete and clear example.

So, See Example

#include <iostream>
using namespace std;

int main()
{
    int i = 0;
    while(i < 10)
    {
        cout << i << endl;
        i++;
    }
}

Explanation of Example

The explanation of C++ While Loop – Looping example is: In this example,

we declare an integer variable named i and initialize with 0 on the same line, and then we use a while loop with the condition i < 10.

Then, we say print the value of i with cout and use with break operator endl to break the line.

Then the last line, we use the increment operator with i, like i++ to show the this Output:

0
1
2
3
4
5
6
7
8
9

Note: Be careful using the condition in the loop, it can destroy your program or computer.

Hoping to see your feedback in the comment section in the future. Our Recent topic is here:

By Usama Arshad

It's me Usama Arshad working as a Lecturer C++. I am a student of BSIT.

Leave a Reply

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