# Control Flow

So far, our programs have executed line-by-line, step-by-step from top to bottom. In the real world however, we might want to

* make decisons based on a condition
* repeat certain actions
* organise logic into reusable pieces

This is where **control flow** comes in

### Conditionals

Conditionals let our program make decisions.

{% code title="main.cpp" %}

```cpp
int score = 85;

if (score == 100) {
    std::cout << "Perfect" << std::endl;
} else if (score >= 90) {
    std::cout << "Grade A" << std::endl;
} else if (score >= 80) {
    std::cout << "Grade B" << std::endl;
} else if (score >= 70) {
    std::cout << "Grade C" << std::endl;
} else {
    std::cout << "Needs improvement" << std::endl;
}
```

{% endcode %}

{% hint style="info" %}
⚠️ Common Pitfall

`=` means assignment while `==` checks for equality

{% code title="" %}

```cpp
int x = 5;      // assignment
x == 5;         // comparison
```

{% endcode %}
{% endhint %}

### Loops

Loops lets us repeat certain blocks of code

#### while loop

A `while` loop keeps running as long as the condition is fufilled.

<table><thead><tr><th width="354.14404296875">Code</th><th>Output</th></tr></thead><tbody><tr><td><pre class="language-cpp" data-title=""><code class="lang-cpp">int x = 1;
while (x &#x3C;= 5) {
    std::cout &#x3C;&#x3C; x &#x3C;&#x3C; std::endl;
    x++;
}
</code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FxKBymX4E6V1SNDYukXx4%2FScreenshot%202026-04-01%20at%2012.19.51%E2%80%AFPM.png?alt=media&#x26;token=4b792ee7-8d1b-4f9a-bacd-098fea30f10a" alt="" data-size="original"></td></tr></tbody></table>

#### for loop

A `for` loop is useful when we know how many times we want to repeat something.

<table><thead><tr><th width="353.73614501953125"></th><th></th></tr></thead><tbody><tr><td><pre class="language-cpp" data-title="main.cpp"><code class="lang-cpp">for (int i = 1; i &#x3C;= 5; i++) {
    std::cout &#x3C;&#x3C; i &#x3C;&#x3C; std::endl;
}
</code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FZRq5RBPAyzI6ixc5XgGT%2Fimage%20(2).png?alt=media&#x26;token=b53fa0d5-34ff-4e82-94eb-825cdf918c80" alt="" data-size="original"></td></tr></tbody></table>

#### break / continue

`break` forces the loop to terminate prematurely.

`continue` skips the rest of the current iteration.

<table><thead><tr><th width="354.77777099609375"></th><th></th></tr></thead><tbody><tr><td><pre class="language-cpp" data-title="main.cpp"><code class="lang-cpp">for (int i = 1; i &#x3C;= 5; i++) {
    if (i == 3) {
        break;
    }
    std::cout &#x3C;&#x3C; i &#x3C;&#x3C; std::endl;
}
</code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FK7HqGtXccMdjALrfnNcu%2FScreenshot%202026-04-01%20at%2012.25.16%E2%80%AFPM.png?alt=media&#x26;token=85103fb1-919f-4871-a7d0-110fed69e3ae" alt="" data-size="original"></td></tr><tr><td><pre class="language-cpp" data-title="main.cpp"><code class="lang-cpp">for (int i = 1; i &#x3C;= 5; i++) {
    if (i == 3) {
        continue;
    }
    std::cout &#x3C;&#x3C; i &#x3C;&#x3C; std::endl;
}
</code></pre></td><td><img src="https://2807223923-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FTUqAJOgHs57S8lmqdxRV%2Fuploads%2FNMyhOKBh9ETE0q59TCIg%2FScreenshot%202026-04-01%20at%2012.24.30%E2%80%AFPM.png?alt=media&#x26;token=3141c03e-6ba0-44b7-be51-56138659e0c9" alt="" data-size="original"></td></tr></tbody></table>

### Functions

Functions let us group code into reusable blocks.

#### Basic Usage

{% code title="main.cpp" %}

```cpp
#include <iostream>

void greet() {
    std::cout << "Hello!" << std::endl;
}

int main() {
    greet();
}
```

{% endcode %}

Here:

* `void` means the function does not return anything
* `greet` is the function name
* `greet()` *calls* the function, causing **Hello!** to be printed.

#### Function Parameters

Our current `greet` function always print the same message (which isn't very flexible). Let's augment it by adding a parameter so that our greeting message can change depending on the person.

{% code title="main.cpp" %}

```cpp
#include <iostream>

void greet(std::string name) {
    std::cout << "Hello, " << name << "!" << std::endl;
}

int main() {
    greet("Benn");
    greet("Bryan");
    greet("Anton Tim");
}
```

{% endcode %}

#### Functions Return Value

A return value is the value the function sends back to the caller after it's done executing. At the call site, you can use the result however you wish.

{% code title="main.cpp" %}

```cpp
#include <iostream>

int add(int x, int y) {
    return x + y;
}

int main() {
    int result = add(3, 4);
    std::cout << result << std::endl;
}
```

{% endcode %}

### Exercise(s)

⭐ Write a function `countdown(int n)` that takes in a number `n` and prints from `n` down to 1.

{% code title="" %}

```shellscript
countdown(5)
5
4
3
2
1
```

{% endcode %}

⭐⭐ Write a program that hardcodes a secret number. It will repeatedly ask the user to guess. Depending on the user's guess, the program will feedback **Too low, Too high** or **Correct.**

{% code title="" %}

```
Guess the number: 50
Too high

Guess the number: 25
Too low

Guess the number: 30
Too low

Guess the number: 42
Correct!
```

{% endcode %}
