codegurukool.com · Free coding tutorials & online compiler

Home › C++ Tutorial › C++ while loop

C++ while loop

Repeat while true.

while checks first; do...while runs at least once.

Example code

#include <iostream>
using namespace std;
int main() {
  int i = 1;
  while (i <= 3) { cout << i << " "; i++; }
  cout << endl;
  return 0;
}

Run this code in the online editor

← C++ if...else and switchC++ for loop →