C++ while loop for beginners

0 0
Read Time:39 Second

The while loop is used when we want the loop to execute and continue executing while the specified condition is true. As the condition is checked at the beginning of the loop, it is also called entry controlled loop.

Syntax:

init-statement
while (condition) {
statement
iteration-expression;
}
Example:

See the program below.

#include <iostream>
using namespace std;

int main() {
   int i = 1;

   //while loop
   while (i <= 5) { 
      cout << "The number is " << i << endl;
      i = i + 1;
   }
   return 0; 
};

Output:

The number is 1
The number is 2
The number is 3
The number is 4
The number is 5

Note: Do not forget to increase the variable used in the condition, otherwise the loop will never end!

Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

One thought on “C++ while loop for beginners

Comments are closed.