# Chapter 6 - Notes

## Conditional Execution

### If-else statements&#x20;

* Conditional execution of code in C++ is done by using the **`if ... else`** construct:

```cpp
if (condition) {
    // do something if the condition is true 
}
else {
    // do something else if the condition is false
}
```

{% hint style="info" %}
Every expression in C++ is **truthy,** meaning that it can be evaluated as a boolean.&#x20;

Anything that evaluates to **zero** is **false**. Conversely, anything that evaluates to **any non-zero number** is **true**.&#x20;
{% endhint %}

* The `else` component of the `if ... else` construct is **optional**. It can be committed in situations where no code is to be executed otherwise.&#x20;
* Nested `if` statements allow the user to validate against two or more conditions.

```cpp
if (day == Sunday) {
    std::cout << "Today is Sunday!" << std::endl;
    if (weather == Rainy) {
        std::cout << "You should stay inside today." << std::endl;
    }
    else {
        std::cout << "You should go outside today!" << std::endl;
    }
}
```

* The `if ... else` constructs can also be grouped together:&#x20;

```cpp
if (day == Friday) {
    std::cout << "Today is Friday!" << std::endl;
}
else if (day == Saturday) {
    std::cout << "Today is Saturday!" << std::endl;
}
else {
    std::cout << "Today is neither Friday nor Saturday..." << std::endl;
}
```

{% hint style="info" %}
What is the difference between using grouped `if ... else` statements as opposed to using multiple chained `if` statements?
{% endhint %}

### Switch-case statements&#x20;

* The `switch-case` construct checks a particular expression against a range of possible constants, conditionally executing code if the expression matches each of the different values.&#x20;

```cpp
switch (expression) {
    case value1:
        // do something if expression == value1 
        break;
    case value2:
        // do something if expression == value2
        break;
    default:
        // do something if all the above cases don't match 
        break;
}
```

* Each case label is required to be a constant.&#x20;
* The `break` keyword causes the program to exit the current code block (the block belonging to the `switch` statement. This is not strictly necessary, but without the `break` statements, the program will continue checking each `case` label, which we want to avoid.
* `switch-case` constructs are ideal for use with enums.&#x20;

{% hint style="info" %}
A program using `switch-case` can be adapted to use grouped `if ... else` instead. The benefit of using the former over the latter is that the syntax of the latter is a little more structured.&#x20;
{% endhint %}

### Ternary operator&#x20;

* The **ternary operator** is an operator that allows us to express an `if .. else` construct in a single line:&#x20;

```cpp
(condition) ? (expression if true) : (expression if false)
```

* The program evaluates the condition before the `?` – and if **`true`**, returns the expression on the **left** of the `:`, and if **false**, returns the expression on the **right** of the `:`

```cpp
int larger = (a > b) ? a : b;
// If `a` is larger than `b`, the ternary operator returns `a`
// If `b` is larger or equal to `a`, the ternary operator returns `b`
```

## Looping

* Loops allow programs to repeat a block of code without repeating the block of code already written&#x20;

### While loops&#x20;

```cpp
while (expression) {
    // do something
}
```

* The block within the while loop will execute repeatedly as long as `expression` evaluates to true.&#x20;
* It is important to structure the code block within such that there are situations where `expression` eventually evaluates to `false`, otherwise the while loop will never end.&#x20;

### Do-while loops

```cpp
do {
    // do something 
} while (expression);
```

* Do-while loops are the same as while loops. The only difference being the condition is evaluated at the **end** of each iteration.&#x20;
* This means that the block within the do-while loops always execute **at least once**.&#x20;

### For loops&#x20;

```cpp
for (initial expression; exit condition; loop expression) {
    // do something 
}
```

* `initial expression`: This expression is executed **once** – before the first iteration of the loop&#x20;
* `exit condition`: This conditional expression is evaluated **before the next iteration of the loop**, and will determine whether the next iteration should run.&#x20;
* `loop expression`: This expression is executed **at the end of every iteration** of the loop.&#x20;

#### Range-based for loop&#x20;

* **Range-based loops** are a variant of the for loop that makes operating over a collection (array or vectors, for example) simpler to code and read&#x20;

```cpp
for (ElementType variable : collection) { 
    // `variable` corresponds to each element in the collection
    // do something with `variable`
}
```

* This is frequently used with automatic type deduction (using the keyword `auto`) for the type of each element in the collection.&#x20;

### Modifying loop behaviour&#x20;

### Nested loops&#x20;
