AppDividend
Latest Code Tutorials

While Loop in C++ Program | C++ While Loop Example

0

While Loop in C++ Program | C++ While Loop Example is today’s topic. For understanding while loop, we must have prior knowledge of loops in C++. Loops are used when we want a particular piece of code to run multiple times. We use loops to execute a statement of codes repeatedly until a specific condition is satisfied. It eases the work of the programmer and also shortens the code length. We have already seen For loop.

While Loop in C++ Program

For example, if we want to print a table of a particular number, we can directly do it with 3-4 lines of code using the loop.

The problem with loops is that it increases the complexity of the code and sometimes we work for an optimized code then we are supposed to look at different methods by which we can solve that problem by using loops.

The thing with the loop is that we have to give parameters of the loop, which includes iterative conditions that are different for different kinds of loops. We need to run the loop and provide iteration conditions.

There are 3 types of loops for loop, while loop and do-while loop. In this tutorial, we will learn about while loop.

While loops are used for executing a block of code repeatedly until the condition returns false.

The syntax for while loop

while(condition)
{
	// Statements
}

See the following code.

k=5;
while(k!=0)
{	cout<<k<<endl;
	k=k-1
}

The above example will print the value of k starting from 5 until it’s value becomes 0 in a new separated line every time.

Flow chart of while loop

 

While Loop in C++ Program

Working of while loop

While loops first evaluate the condition and if it returns true, then the programming statements which are inside the loop get executed. This keeps on going until the condition inside while loop returns false.

When the condition returns false, the control comes out of the loop.

Difference between for loop and while loop:

The main difference between for loop and while loop is that in while loop we are supposed to use increment and decrement counter inside the loop so that the loop variables get changed on each iteration, and after successfully running the loop it returns true whereas in for loop we use increment and decrement counter in the parameter section of the code.

Let’s see the following example.

In case of for loop:  for(int i=0;i<5;i++)

In case of while loop:

while(k!=0)
{     
   cout<<k<<endl;
   k=k-1;
}

Example program for while loop in C++

Q1- Write a program to show the mechanism of a while loop.

#include<iostream>
using namespace std;
int main()
{
	int t=5;
	cout<<"Hello I am going to print the value of variable t in decending order"<<endl;
	while(t!=0)
	{
		cout<<t<<endl;
		t=t-1;
	}
	return(0);
}

See the following output.

 

Example program for while loop in C++

Q2- Write a program to print the table of 10 using a while loop.

#include<iostream>
using namespace std;
int main()
{
		int t=1,k=10;
		cout<<"Table of 10"<<endl;
		while(t!=11)
		{
			cout<<"10 X "<<t<<"="<<k*t<<endl;
			t=t+1;
		}
		return(0);
}

See the output.

 

while loop tutorial

Infinite While loop

The while loop that never stops, is said to be an infinite while loop, when we give a condition in such a way so that it never returns false, then a loop becomes an infinite and repeats itself indefinitely.

An example of infinite while loop:
While loop would never end as I’m decrementing the value of i, which is one so the condition i<=6 would never return false.

#include <iostream>
using namespace std;
int main(){
   int i=1; while(i<=6) {
      cout<<"Value of variable i is: "<<i<<endl; i--;
   }
}

Displaying the items of an array using a while loop

See the following code that displays the elements of an array using a while loop.

#include <iostream>
using namespace std;
int main(){
   int arr[]={21,87,15,99, -12};
   /* The array index starts with 0, the
    * first element of array has 0 index
    * and represented as arr[0]
    */
   int i=0;
   while(i<5){
      cout<<arr[i]<<endl;
      i++;
   }
}

See the output.

21
87
15
99
-12

Finally, While Loop in C++ Program | C++ While Loop Example is over.

Recommended Posts

C++ Friend Function Example

C++ Destructor Example

C++ String Class Tutorial

C++ Structure Example

Virtual Function in C++ Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.