Let us now take a look at another example where you can use a while loop.
You can download the file below which will help you through the following video.
Here I am trying to declare a variable int NUM starting from one and I try to print all the numbers from one to 100. So I say I first declare an integer NUM outside of this while loop. I say int NUM is equal to one. Then I say that while NUM is less than equal to 10. Notice that this is less than equal to and not less than. Why is that? Because I need to print all the numbers from one to 10 including one and 100 both. Therefore, my while loop will get executed till NUM reaches 100. Therefore I write While NUM is less than equal to 100. Then I print out the value of NUM and in the next step I increment my NUM variable. Remember that this is another way of updating my NUM variable. If instead of this I would have written something like this NUM is equal to NUM plus one, then it would also have been the same statement. So basically saying that this statement is equivalent, this statement is equivalent to this statement. This is just an alternative way of updating the NUM value by one. So let me delete this statement. Let us recheck our program. We define a variable NUM outside which starts from one and goes till 100 including 100. And I print out the value of NUM. And in the next step I update my NUM also. So let us try to run this program and see what it prints on the console. See on the console, beginning from one, two, three, all the numbers between one to 10 are printed including the last number which is 100. Let us now see what would have happened if I would have forgotten to add my equal to sign here. So I remove the equal to sign and then I run this so you can see that all the numbers starting from one, two, three get printed till number 99. And why does 100 not get printed? Well, this happens because when NUM is equal to 99, this while condition holds true. Since 99 is less than 100, then 99 gets printed on the console. And in the next step, NUM value reaches 100. As soon as NUM value reaches 100, then when the next time while loop is executed, it finds that since 100 is not less than 100, therefore this while condition no longer holds true and this while loop is broken. That is, we come out of the while loop.
Now, there are few things which you need to be careful while writing any while loop. One common mistake is to be able to forget to initialize this particular variable. You must always remember to initialize whichever variable you are declaring outside the while loop. Similarly, you should make sure that the while loop terminates. That is, the while loop runs only a finite number of times. So you have to be very careful while writing the condition which is there inside the while bracket. And lastly, in order to avoid infinite loops, you must never forget to update the value of your changing variable in this while loop. My NUM variable is the changing variable. So I must remember always to update my value of NUM. Suppose if I had forgotten to update the value of NUM, I remove the statement here. Then you would see that the program runs into an infinite loop. See, one is getting printed infinite number of times. You can see the moving scroll bar here. So basically, one is getting printed a large number of times. This while loop is not getting terminated. It is because I forgot to update my value of NUM. Therefore, NUM remains stuck only at one. This condition always holds true, and it keeps on printing one an infinite number of times. Let me stop this program by clicking on the stop icon here. Yeah. So basically, you need to always update whichever variable you're changing inside the while loop.
Learn how to use while loops in C++ programming with this tutorial. Declare a variable int NUM and print all numbers from 1 to 100 using a while loop. Initialize the variable outside of the loop and update it inside. Be careful to initialize the variable, write a condition for the loop, and make sure it terminates to avoid infinite loops. The segment shows what happens when you forget to update the variable, and provides tips to avoid common mistakes.
You learned how to print numbers in a sequence in a while loop. There is a simpler method to accomplish this via something called the “for” loop. You will learn more about the for loop in the upcoming segment.
Let us now take a look at another case where while loop will be very handy in reducing the amount of redundant code we would need to write.
Please download the file below which will help you understand the upcoming video better.
Note: The program shown to calculate factorial considers only numbers from 1 to 10. i.e. it is calculating the value of "10!".
You have now learned how a while loop works. A set of statements is executed until a condition is met. The condition can be such that the loop is executed a specific number of times and a counter is incremented at every execution.
Let us take a look at a case we would wish to avoid, an infinite while loop.