Loops
Anonymous contributor
Anonymous contributor3077 total contributions
Anonymous contributor
Published May 6, 2021Updated Mar 27, 2023
Contribute to Docs
A loop can execute a statement or group of statements multiple times and the following is the general from of a loop statement in most programming languages.
While Loop
A while
loop statement repeatedly executes the code block within as long as the condition is true. The moment the condition becomes false, the program will exit the loop.
Note: The
while
loop might not ever run. If the condition is initiallyfalse
, the code block will be skipped.
while (password != 1234) {std::cout << "Try again: ";std::cin >> password;}
For Loop
A for
loop executes a code block a specific number of times. It has three parts:
- The initialization of a counter
- The continue condition
- The increment/decrement of the counter
This example prints 0 to 9 on the screen:
for (int i = 0; i < 10; i++) {std::cout << i << "\n";}
All contributors
- Anonymous contributorAnonymous contributor3077 total contributions
- Prince2517 total contributions
- Christine_Yang269 total contributions
- garanews209 total contributions
- christian.dinh2481 total contributions
- Anonymous contributor
- Prince25
- Christine_Yang
- garanews
- christian.dinh
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.