Labs ICT
๐ŸŽ“ SIWES/IT Login

Do...While Loop

1 min read | C++ Tutorial
๐ŸŽ“

Doing SIWES / IT attachment?

Get a structured training programme, a supervised digital logbook, and a completion certificate with LabsICT SIWES / IT.

Explore SIWES / IT Training

The do-while Loop

The do-while loop is like while, but it guarantees the body executes at least once before checking the condition.

int x = 10;

do {
  cout << x << "\n";
  x--;
} while (x > 0);

This is perfect when the first iteration must always happen โ€” for example, showing a menu before asking the user to pick an option.

The condition comes after the body, so remember the semicolon at the end.

int secret = 7;
int guess;

do {
  cout << "Guess: ";
  cin >> guess;
} while (guess != secret);

cout << "Correct!";

๐Ÿงช Quick Quiz

Which loop is guaranteed to run at least once?