Home >>C++ Tutorial >C++ While Loop
In order to iterate a part of the program numerous times, the while loop in C++ is used. It is generally recommended to use the while loop instead of for loop when the number of iterations are not certain/defined.
Here is the syntax of the while loop in C++
while(condition) { //code to be executed }
Here is an example of the while loop that will print the table of 10
#include <iostream> using namespace std; int main() { int num; cout<<"Enter your number: "; cin>>num; int i=1; int table=0; while(i<=10) { table=num*i; cout<<table <<"\n"; i++; } return 0; }
A while loop can be used inside another while loop and it is called as the Nested While loop in C++ programming. Whenever the outer loop that is created gets executed once then the nested loop gets executed completely.
Syntax of Nested while loop
#include <iostream> using namespace std; int main () { while(condition) { while (condition) { //statement to be executed } } }