While Loop in C++ Programming Language
While Loop in C++ Programming Language
We have discussed about the entry-controlled loop or pre-tested loop previously in our blog posts. These statements or loops are used to repeat a piece of code for a specific number of times as per our program requirement.
We have studied about the for loop already. Today in this blog post we are going to study about the while loop in C++ programming language. And we will look at how this while loop actually works and how we can stop it by making some condition false so that the loop might not run infinitely.
We use while loop in C++ program when the number of iterations is not known in advance and depends on the condition. When we know exactly that how many times a loop should run then we use the for loop instead of while loop.
Basic Syntax Of While Loop in C++ Programming Language
Now we are going to discuss about the basic working of while loop in C++ programming. As we know that while loop is pre-tested loop and we should use it when we dont know that how many times the loop will run.
Basically in while loop first we need a variable that will start and terminate the while loop Based on some given condition like this
int i = 1;
while(i <= 10)
{
//some piece of code
i++;
}
Once we have declared and initialized the variable, after that we set some condition on the basis of which our loop will run and terminate. At the end of while loop before the ending curly brackets of while loop we usually increament the value of that variable so that at some point our loop will stop with respect to the given condition, like in the above given example while loop will run 10 times as variable i has value 1 and when the value of i will become 10 the loop will terminate.
Simple Programming Example Of While Loop In C++ Programming Language
We are going to look at two example codes in which we have used while loop
In the first coding example we have declared and initialized a variable i with value 1 i.e, int i = 1;
And after that we have used this variable inside the while loop expression body with in the round brackets i.e, while(i<=10) in this condition we mentioned that this loop should stop when value of i is less or equal to 10 and at the end of while loop we simply increment the value of i by 1 i.e, i++ which us equal to i = i +1;
So this loop will run or repeat 10 times and after that it will terminate.
Now we are going to look at second while loop coding example.
In the second coding example we simply took two inputs from the user the first one is TableNumber and the Second one is the TableLength. In this program we are generating table of any specific number and the table would be of some specific number of length. And we are going to achieve this by using while loop.
The TableNumber will indicate the number for which we want to see the table of and the TableLength will decide that the length of table should be to some specific table length i.e if TableNumber = 2 and TableLength = 10 then the while loop will print the table of 2 from 1 to 10
2 * 1 = 2
2 * 2 = 4
2 * 3 = 6
.
.
.
2 * 10 = 20
And in the while loop we simply give the condition, that the loop should run when the value of i is less or equal to the TableLength.
In this way our program will execute successfully.
Congratulations like always we have successfully executed the while loop program in C++ Programming language.


Comments
Post a Comment