Tutorials

C++ Tutorial

All Tutorials Learning Tools Login

C++ Tutorial · Chapter 10 of 13

C++ While Loop

Condition-controlled repetition.

while is useful when the stop condition depends on input or a changing value.

Always update the condition variable inside the loop.

While sample Try it Yourself »
#include <iostream>
using namespace std;

int main() {
    int i = 1;
    while (i <= 5) {
        cout << i << endl;
        i++;
    }
    return 0;
}

Classroom Tips

  • Watch out for infinite loops.