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 and working of while loop in C++ programming language


Syntax of while loop

int i = 1;
while (condition)
{
	// code of block to be executed here.
	// some variable to be incremented i++.
}


Working Of While Loop In C++ Programming Language

Let us look at the syntax of while loop in C++ programming language. In the first line we just declared and initialized a variable name i. After that we have used the while keyword followed by the opening and closing round brackets with some specific condition written inside it. After that we have opening and closing curly brackets with in which we are going to write the programming logic. And remember that at the end of while loop we are going to increment or decrement the value of variable i depending upon the logic of our while loop. If the condition is true the while loop will run and if the condition is false the while loop while loop will terminate.

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 


While loop coding example 1

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int i = 1;
	while (i <= 10)
	{
		cout << i << endl;
		i++;
	}
	_getch();
}


Explanation of coding example 1

In the first coding example we have declared and initialized a variable i with value 1 i.e, int i = 1;

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) 

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 is equal to i = i +1;

cout << i << endl;
i++;

So this loop will run or repeat 10 times and after that it will terminate.


While loop coding example 2

#include<iostream>
#include<conio.h>
using namespace std;
void main()
{
	int TableNumber, TableLength, i = 1;
	cout << "Enter table number = ";
	cin >> TableNumber;
	cout << "Enter table length = ";
	cin >> TableLength;
	while (i <= TableLength)
	{
		cout << TableNumber << " * " << i << " = " << TableNumber * i << endl;
		i++;
	}
	_getch();
}


Explanation of coding example 2

In the second coding example we simply take two inputs from the user the first one is TableNumber and the Second one is the TableLength. 

int TableNumber, TableLength, i = 1;
cout << "Enter table number = ";
cin >> TableNumber;
cout << "Enter table length = ";
cin >> 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 range of numbers i.e if TableNumber = 2 and TableLength = 10 then the while loop will print the table of 2 from 1 to 10

while (i <= TableLength)
{
	cout << TableNumber << " * " << i << " = " << TableNumber * i << endl;
	i++;
}


2 * 1 = 2

2 * 2 = 4

2 * 3 = 6

2 * 4 = 8

2 * 5 = 10

2 * 6 = 12

2 * 7 = 14

2 * 8 = 16

2 * 9 = 18

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 implemented the while loop in C++ Programming language.


Comments

Popular posts from this blog

How to Download and Install Microsoft visual studio community 2022 for free

For Loop In C++ Programming Language

Hello World Program In C Programming Language