> For the complete documentation index, see [llms.txt](https://university-west.gitbook.io/programming-paradigms/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://university-west.gitbook.io/programming-paradigms/programming-fundamentals/operators/logical.md).

# Logical

#### Logical Operators in Computer Programming

Logical operators are used to perform logical operations on boolean values (true or false). They are fundamental in controlling the flow of execution in programs, especially in conditional statements and loops. The most common logical operators are AND, OR, and NOT.

**Types of Logical Operators:**

1. **AND (`&&` or `and`):** Returns true if both operands are true. Otherwise, it returns false.
   * **Example:** `true && true` is `true`, but `true && false` is `false`.
2. **OR (`||` or `or`):** Returns true if at least one of the operands is true. If both operands are false, it returns false.
   * **Example:** `true || false` is `true`, and `false || false` is `false`.
3. **NOT (`!` or `not`):** Inverts the value of the operand. If the operand is true, it returns false, and vice versa.
   * **Example:** `!true` is `false`, and `!false` is `true`.

**Examples in Programming Context:**

Consider the following pseudocode examples demonstrating the use of logical operators:

```python
# Example variables
a = true
b = false

# Logical AND
result1 = a && b  # result1 will be false, as both operands are not true

# Logical OR
result2 = a || b  # result2 will be true, as at least one operand is true

# Logical NOT
result3 = !a  # result3 will be false, as it inverts the value of a
```

**Usage:**

Logical operators are particularly useful in conditions and control statements:

* **Conditional Statements:** In `if` statements, logical operators can combine multiple conditions. For example, `if (age > 18 && hasLicense) { drive(); }`.
* **Loops:** While or for loops can use logical operators to determine the continuation condition. For instance, `while (isRunning && !isError) { process(); }`.

**Points to Note:**

* **Short-Circuit Evaluation:** In many languages, logical AND and OR operators perform short-circuit evaluation. For `&&`, if the first operand is false, the second operand is not evaluated. For `||`, if the first operand is true, the second operand is not evaluated.
* **Boolean Context:** Some languages, like Python, evaluate non-boolean types in a boolean context. For instance, `0`, `None`, `""` (empty string) are considered false.
* **Precedence:** Logical operators have specific precedence rules. Typically, NOT has a higher precedence than AND, which has a higher precedence than OR.

Logical operators are essential in creating complex conditional expressions and are integral to the logic of most programs. They enable a program to make decisions and perform actions based on various conditions.


---

# 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://university-west.gitbook.io/programming-paradigms/programming-fundamentals/operators/logical.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.
