while loop in C++

Hello Readers,

In the previous tutorial, we studied about loops, and specifically, for loops. Today, we’ll study while loops.

A while loop in C++ is very much like a for loop and works in almost the same way. We’ll see later the difference b/w the both.

The syntax of both is different, but the working is almost same. In while loop, there is only 1 condition, which is checked in every iteration. The code inside the loop gets executed till the condition is true.

If you’re not able to understand the above lines, don’t worry, things will get sorted out once we move ahead with this tutorial.

Note: If you already know C++, then you can switch to the practice part.

Syntax of while loop

while(condition){
    // code statement
}

The initialisation can be done before the starting of while loop. One can easily understand that till the condition inside the loop is true, the code will keep on executing.

I believe that practical learning is the best learning. So, let’s print the numbers from 1-5 using while loop, and see the output.

#include <iostream>

using namespace std;

int main(){

    int i = 1; // initialisation

    while(i < 6){ // condition

	cout<<i<<endl;

	i++; // iteration
    }

    return 0;
} 

Output

1
2
3
4
5

That’s how, we can execute the while loop. The iteration part can be done inside the code of while loop.

I want all of you to actually run the above code on some ide, and ask the doubt if any in the comment section.

Note: If you’re confused which IDE to choose, let us clear your confusion

Well, since you’ve ran your code, let’s move ahead and see the control structure of the while loop.

Control Structure of while loop

Below is the control structure of the while loop. This is how while loop actually works internally.

flow control of while loop in c++
  • Save
flow control of while loop in c++

Since, till now we have seen all the important concepts related to both while loop and for loop. So, many of you may ask about the difference between the two.

Difference between for loop & while loop

for loopwhile loop
Initialisation
for(initialisation;condition;iteration){
// code statement
}
Initialisation
while(condition){
// code statement
}
If you put no condition inside the for loop, it will run infinite timesIf you put no condition in while loop, it will give compilation error.
In for loop, the iteration part comes after the code.In while loop, the iteration part can come within the while loop
Difference for loop & while loop in C++

So, the above are some of the few small differences between for loop & while loop. The most important difference is 2nd one.

Note: Any loop can be used to solve a problem.

So, here is all about the while loop. Since, we have also learned about for loop, I highly recommend you to go through the problems <here>. These will not only increase your control on the loops and if statement but also increase your logic building skills.

Logic building is indeed very much important since it helps you to get good placement but also makes your approach solution oriented which is very important to achieve something in life.

The next tutorial will be on do-while loop. It will be out soon. Till then, stay tuned & please subscribe to the newsletter.

Leave a Reply

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

Share via
Copy link
Powered by Social Snap