Skip to content

While Loop

Artemis edited this page May 13, 2023 · 2 revisions

C++ While Loop

The while loop is a type of loop used to execute a certain set of instructions or code until a certain condition is met.

Syntax

The syntax for a while loop in C++ is as follows:

while (condition) {
  // code to be executed
}

Where condition is a boolean expression.

Example

Here is an example of a while loop in C++:

int x = 0;
while (x < 10) {
  cout << x << endl;
  x++;
}

This code will print out the numbers from 0 to 9 and then exit the loop.

Break and Continue

The break and continue statements can be used inside a while loop. The break statement will exit the loop and the continue statement will skip the current iteration and continue with the next iteration.

Conclusion

The while loop is a powerful tool for executing a set of instructions until a certain condition is met. It can be used in combination with the break and continue statements to control the flow of the loop.

Clone this wiki locally