> For the complete documentation index, see [llms.txt](https://alham-rizvi.gitbook.io/alhamrizvi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/11.-conditionals-if-else-else-if.md).

# 11. Conditionals — if, else, else if

##

Conditionals let your program make decisions — run different code depending on whether something is `true` or `false`.

***

### `if` / `else` — your age checker

```cpp
#include <iostream>

int main() {
    int age;
    std::cout << "your age: ";
    std::cin >> age;

    if (age >= 18) {
        std::cout << "welcome to horn pub\n";
    }
    else {
        std::cout << "noobs not allowed\n";
    }

    return 0;
}
```

**Example runs:**

```
your age: 21
welcome to horn pub
```

```
your age: 15
noobs not allowed
```

***

### Breaking it down

| Part             | Meaning                                                                             |
| ---------------- | ----------------------------------------------------------------------------------- |
| `if (condition)` | If the thing inside `()` evaluates to `true`, run the block below it.               |
| `{ ... }`        | The code that runs when the condition is true.                                      |
| `else { ... }`   | The fallback — runs only if the `if` condition was `false`.                         |
| `age >= 18`      | A **comparison operator** (see topic 03/operators) that produces `true` or `false`. |

You don't always need `else` — an `if` on its own is totally valid if there's nothing to do otherwise:

```cpp
if (age >= 18) {
    std::cout << "welcome\n";
}
// program just continues here if age < 18, no else needed
```

***

### `else if` — checking multiple conditions in order

When you have more than two possible outcomes, chain conditions with `else if`. C++ checks them **top to bottom** and stops at the first one that's `true`.

```cpp
#include <iostream>

int main() {
    int age;
    std::cout << "your age: ";
    std::cin >> age;

    if (age < 13) {
        std::cout << "you're a kid\n";
    }
    else if (age < 20) {
        std::cout << "you're a teenager\n";
    }
    else if (age < 65) {
        std::cout << "you're an adult\n";
    }
    else {
        std::cout << "you're a senior\n";
    }

    return 0;
}
```

**Important:** once one branch runs, C++ skips the rest — even if a later condition would also be true. Order matters.

***

### Comparison & logical operators (the building blocks of conditions)

| Operator  | Meaning                             |
| --------- | ----------------------------------- |
| `==`      | Equal to                            |
| `!=`      | Not equal to                        |
| `>` `<`   | Greater / less than                 |
| `>=` `<=` | Greater or equal / less or equal    |
| `&&`      | AND — both sides must be true       |
| \|\|      | OR — at least one side must be true |
| `!`       | NOT — flips true/false              |

```cpp
if (age >= 18 && hasID) {
    std::cout << "allowed in\n";
}
```

***

### More cool examples

#### 1. Login check with `&&`

```cpp
#include <iostream>
#include <string>

int main() {
    std::string username, password;

    std::cout << "Username: ";
    std::cin >> username;
    std::cout << "Password: ";
    std::cin >> password;

    if (username == "admin" && password == "1234") {
        std::cout << "Access granted\n";
    }
    else {
        std::cout << "Access denied\n";
    }

    return 0;
}
```

#### 2. Grading system with `else if` chain

```cpp
#include <iostream>

int main() {
    int score;
    std::cout << "Enter score: ";
    std::cin >> score;

    if (score >= 90) {
        std::cout << "Grade: A\n";
    }
    else if (score >= 80) {
        std::cout << "Grade: B\n";
    }
    else if (score >= 70) {
        std::cout << "Grade: C\n";
    }
    else {
        std::cout << "Grade: F\n";
    }

    return 0;
}
```

#### 3. Checking a range with `&&`

```cpp
#include <iostream>

int main() {
    int temperature;
    std::cout << "Enter temperature: ";
    std::cin >> temperature;

    if (temperature >= 15 && temperature <= 25) {
        std::cout << "Nice weather!\n";
    }
    else {
        std::cout << "Not ideal weather\n";
    }

    return 0;
}
```

#### 4. Using `||` for multiple accepted answers

```cpp
#include <iostream>
#include <string>

int main() {
    std::string answer;
    std::cout << "Do you agree? (yes/y): ";
    std::cin >> answer;

    if (answer == "yes" || answer == "y") {
        std::cout << "Great, moving on...\n";
    }
    else {
        std::cout << "Okay, cancelled\n";
    }

    return 0;
}
```

#### 5. Nested `if` (an `if` inside another `if`)

```cpp
#include <iostream>

int main() {
    int age;
    bool hasTicket;

    std::cout << "Enter age: ";
    std::cin >> age;
    std::cout << "Do you have a ticket? (1 = yes, 0 = no): ";
    std::cin >> hasTicket;

    if (age >= 18) {
        if (hasTicket) {
            std::cout << "Welcome in!\n";
        }
        else {
            std::cout << "You need a ticket first\n";
        }
    }
    else {
        std::cout << "You're too young to enter\n";
    }

    return 0;
}
```

> Nested `if`s work, but get messy fast — often `&&` can flatten them: `if (age >= 18 && hasTicket)`.

***

### Quick Recap

* `if (condition) { ... }` — runs code only if `condition` is `true`.
* `else { ... }` — the fallback when the `if` is `false`.
* `else if (condition) { ... }` — check additional conditions in order; the first `true` one wins, the rest are skipped.
* Build conditions with comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`) and combine them with `&&` (AND) / `||` (OR) / `!` (NOT).
* You can nest `if`s inside each other, but `&&` often keeps things cleaner.
