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++
Flow chart diagram of While loop 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++
using namespace std;
// this program will print 'I am a programmer' we enter x
cout<<"Enter a character:"<<endl;
cout<<"I am a programmer "<<"Enter a character again: ";