while loop in c++
- A while loop in C++ programming repeatedly executes a target statement as long as a given condition is true.
- Its called a loop because control keeps looping back to the start of the statement until the test becomes false.
- The loop iterates as long as the defined condition is true. When it ceases to be true and becomes false, control passes to the first line after the loop.
- The reserved word while begins the while statement.
- The Boolean expression condition determines whether the body will be (or will continue to be) executed. The expression must be enclosed within parentheses as shown.
- The statement is the statement to be executed while the Boolean expression is true. The statement makes up the body of the while statement.
#include using namespace std;
int n = 99; // make sure n isn’t initialized to 0
while( n != 0 ) // loop until n is 0
cin >> n; // read a number into n
cout << endl;