Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Loops #6399

Merged
merged 5 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 50 additions & 21 deletions content/cpp/concepts/loops/loops.md
Original file line number Diff line number Diff line change
@@ -1,45 +1,74 @@
---
Title: 'Loops'
Description: 'C++ has two types of loops: For and While. Loops execute the same group of statements multiple times until the condition is no longer true.'
Description: 'Loops provide a way to execute a block of code multiple times, making programs more efficient and concise.'
Subjects:
- 'Code Foundations'
- 'Computer Science'
- 'Game Development'
Tags:
- 'Algorithms'
- 'Control Flow'
- 'Iterators'
- 'Loops'
- 'While'
- 'For'
CatalogContent:
- 'learn-c-plus-plus'
- 'paths/computer-science'
---

A **loop** can execute a statement or group of statements multiple times and the following is the general form of a loop statement in most programming languages.
​In C++ programming, executing a task multiple times without **loops** requires duplicating code, which is inefficient and hard to maintain. For example, displaying a message 10 times would involve writing the same `cout` statement repeatedly:

## 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.
```cpp
#include <iostream>
using namespace std;

> **Note:** The `while` loop might not ever run. If the condition is initially `false`, the code block will be skipped.
int main() {
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;
cout << "This is a text" << endl;

```cpp
while (password != 1234) {
std::cout << "Try again: ";
std::cin >> password;
return 0;
}
```

## For Loop
This method is time-consuming, error-prone, and difficult to maintain. To address this, C++ provides **loops**, control structures that execute a block of code repeatedly based on a condition. By using loops, code becomes more concise, efficient, and easier to manage. They are one of the fundamental building blocks in programming that help solve problems involving repetition.

## How Loops Work

![A flowchart illustrating the general working of a loop](https://raw.githubusercontent.com/Codecademy/docs/main/media/loop-flow-diagram.png)

A `for` loop executes a code block a specific number of times. It has three parts:
The general flow of a loop involves:

- The initialization of a counter
- The continue condition
- The increment/decrement of the counter
1. An initialization (setting up a starting point)
2. A condition check (determining whether to run the loop)
3. Code execution (running the statements inside the loop)
4. An update (modifying values before checking the condition again)

This example prints 0 to 9 on the screen:
Using the earlier example, we can use a loop to display "This is a text" 10 times with just a few lines of code:

```cpp
for (int i = 0; i < 10; i++) {
std::cout << i << "\n";
#include <iostream>
using namespace std;

int main() {
for (int i = 0; i < 10; i++) {
cout << "This is a text" << endl;
}

return 0;
}
```

## Types of Loops in C++

C++ provides several types of loops, each with its own use cases:

- **for loop**: Used when the number of iterations is known beforehand.
- **while loop**: Executes as long as the specified condition remains true.
- **do-while loop**: Similar to while loop, but guarantees at least one execution of the code block.
- **range-based for loop**: Introduced in C++11, it simplifies iteration over elements in arrays, vectors, and other containers.
Binary file added media/loop-flow-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.