> 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/15.-logical-operators-and-and-or-or.md).

# 15. Logical Operators — &&, ||, !

Logical operators let you combine or reverse conditions, so you can check for more than one thing at once.

| Operator | Name | Meaning                                       |
| -------- | ---- | --------------------------------------------- |
| `&&`     | AND  | `true` only if **both** sides are `true`      |
| \|\|     | OR   | `true` if **at least one** side is `true`     |
| `!`      | NOT  | Flips `true` to `false` and `false` to `true` |

***

### AND (`&&`)

Both conditions must be `true` for the whole thing to be `true`.

```cpp
#include <iostream>

int main() {
    double 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;
}
```

**Truth table for `&&`:**

| A     | B     | A && B   |
| ----- | ----- | -------- |
| true  | true  | **true** |
| true  | false | false    |
| false | true  | false    |
| false | false | false    |

***

### OR (`||`)

Only **one** side needs to be `true`.

```cpp
#include <iostream>

int main() {
    int day;
    std::cout << "Enter day number (1-7): ";
    std::cin >> day;

    if (day == 6 || day == 7) {
        std::cout << "It's the weekend!\n";
    }
    else {
        std::cout << "It's a weekday\n";
    }

    return 0;
}
```

**Truth table for `||`:**

| A     | B     | A \|\| B |
| ----- | ----- | -------- |
| true  | true  | **true** |
| true  | false | **true** |
| false | true  | **true** |
| false | false | false    |

***

### NOT (`!`)

Flips a `true`/`false` value to its opposite.

```cpp
#include <iostream>

int main() {
    bool isRaining = false;

    if (!isRaining) {
        std::cout << "Go outside!\n";
    }
    else {
        std::cout << "Stay in\n";
    }

    return 0;
}
```

`!isRaining` reads as "not raining" — since `isRaining` is `false`, `!isRaining` becomes `true`.

***

### Combining them

You can mix `&&`, `||`, and `!` in one condition. Use parentheses to keep the logic clear (and to control order of evaluation, just like in math).

```cpp
#include <iostream>

int main() {
    int age;
    bool hasID;

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

    if (age >= 18 && hasID) {
        std::cout << "You're allowed in\n";
    }
    else if (age >= 18 && !hasID) {
        std::cout << "You need to bring ID\n";
    }
    else {
        std::cout << "You're too young\n";
    }

    return 0;
}
```

***

### More cool examples

#### 1. Valid password check (length AND contains a number — simplified)

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

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

    bool longEnough = password.length() >= 8;

    if (longEnough && password != "password") {
        std::cout << "Password accepted\n";
    }
    else {
        std::cout << "Password too weak\n";
    }

    return 0;
}
```

#### 2. Discount eligibility (OR — either condition qualifies)

```cpp
#include <iostream>

int main() {
    int age;
    bool isStudent;

    std::cout << "Enter age: ";
    std::cin >> age;
    std::cout << "Are you a student? (1 = yes, 0 = no): ";
    std::cin >> isStudent;

    if (age >= 65 || isStudent) {
        std::cout << "You qualify for a discount!\n";
    }
    else {
        std::cout << "No discount available\n";
    }

    return 0;
}
```

#### 3. Using `!` to guard against bad input

```cpp
#include <iostream>

int main() {
    int num;
    std::cout << "Enter a positive number: ";
    std::cin >> num;

    if (!(num > 0)) {
        std::cout << "That's not positive!\n";
        return 1;
    }

    std::cout << "Thanks, got: " << num << '\n';

    return 0;
}
```

#### 4. Traffic light logic (multiple `&&`/`||` combined)

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

int main() {
    std::string light;
    bool pedestrianWaiting;

    std::cout << "Light color (red/yellow/green): ";
    std::cin >> light;
    std::cout << "Pedestrian waiting? (1 = yes, 0 = no): ";
    std::cin >> pedestrianWaiting;

    if (light == "green" && !pedestrianWaiting) {
        std::cout << "Go\n";
    }
    else if (light == "yellow" || (light == "green" && pedestrianWaiting)) {
        std::cout << "Slow down\n";
    }
    else {
        std::cout << "Stop\n";
    }

    return 0;
}
```

***

### Practice Program — Temperature Conversion

Combines logical operators + `if` statements to build a two-way Fahrenheit ⇄ Celsius converter.

```cpp
#include <iostream>

int main() {
    char unit;
    double temp;

    std::cout << "Convert from (C)elsius or (F)ahrenheit? ";
    std::cin >> unit;

    std::cout << "Enter temperature: ";
    std::cin >> temp;

    if (unit == 'C' || unit == 'c') {
        double fahrenheit = (temp * 9 / 5) + 32;
        std::cout << temp << "C is " << fahrenheit << "F\n";
    }
    else if (unit == 'F' || unit == 'f') {
        double celsius = (temp - 32) * 5 / 9;
        std::cout << temp << "F is " << celsius << "C\n";
    }
    else {
        std::cout << "Invalid unit entered\n";
    }

    return 0;
}
```

**Example run:**

```
Convert from (C)elsius or (F)ahrenheit? C
Enter temperature: 100
100C is 212F
```

**Breaking it down:**

| Part                           | Meaning                                                            |
| ------------------------------ | ------------------------------------------------------------------ |
| `unit == 'C' \|\| unit == 'c'` | Accepts both uppercase and lowercase input — OR lets either match. |
| `(temp * 9 / 5) + 32`          | Celsius → Fahrenheit formula.                                      |
| `(temp - 32) * 5 / 9`          | Fahrenheit → Celsius formula.                                      |
| final `else`                   | Catches anything that isn't C/c/F/f.                               |

***

### Quick Recap

* `&&` (AND) — both sides must be true.
* `||` (OR) — at least one side must be true.
* `!` (NOT) — flips true ↔ false.
* Combine them with parentheses to build more precise conditions.
* Practice project: a Celsius ⇄ Fahrenheit converter using `||` to accept both letter cases, and `if/else if/else` to route the calculation.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://alham-rizvi.gitbook.io/alhamrizvi/c++-notes/15.-logical-operators-and-and-or-or.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
