C++ While Loop Control Structure

In this Tutorial we will understand and learn the working of While Loop statements in C++ programming language.

While Loop statements in C++ is an iterative looping control structure that executes and block of code until a certain condition is true.
Unlike For Loop, it is not necessary to know the number of iterations before hand. While conditions can contain conditional statements and the Loop will run until those statements are TRUE

Syntax of While Loop statements in C++
while(condition){
   statement(s);
}
Flow chart diagram of While loop in C++

 

While Loop statements in C++

Working of While Loop
  • The while loop evaluates the test condition
  • If the test condition is true, code inside the body of while loop is executed, else the flow control exits the loop block.
  • After first iteration, the test condition is evaluated again. This process goes on until the test condition is false.
  • When the test condition is false, while loop is terminated.
While Loop sample program in C++

Run Online

#include<iostream>
using namespace std;
int main()
{	
	// this program will print 'I am a programmer' we enter x
        char myChar;
	cout<<"Enter a character:"<<endl;
	cin>>myChar;
	
	while(myChar=='x')
	{
		cout<<"I am a programmer "<<"Enter a character again: ";
		cin>>myChar;
	}
	
	return 0;
}
Watch it on YouTube

Leave a Reply

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