As we know, loop is that condition in which a statement or a section of code is repeated number of times until the particular condition or number of conditions met.
Or, simply, while loop executes a set of statements as long as the condition is true.
While loop repeats a specific block of code number of times until the particular condition is met.
Examples of while loop
Pseudocode for while loop
1 2 3 4 5 6 7 8 9 | algorithm while_loop set i = 1 while (i < 6) OUTPUT i increase i by 1 end while end while_loop |
1 2 3 4 5 6 | i = 1 while i < 6: print(i) i = i+1 |
1 2 3 4 5 6 7 | int i = 1; while (i < 6) { System.out.println(i); i++; } |
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> int main(){ using namespace std; int i = 1; while( i <= 6){ cout << n << endl; n++; } return 0; } |