What this guide covers
This is a practical reference for the and, or, not symbols and their roles in logic, programming, databases, and search. These operators let you combine, test, and invert conditions to make precise decisions and queries. You will find concise rules, truth tables, common mistakes, and patterns you can apply immediately. The aim is durable understanding you can reuse across technical tasks and everyday problem solving.
Core definitions and notation
In logic and most programming languages, and, or, not correspond to formal connectives that build compound statements from simpler ones. In SQL, shell commands, and search engines, the words AND, OR, NOT and their symbol equivalents serve the same intent. Common symbols include && for and, || for or, and ! for not. Visual cues help: truth tables map every possible input to its correct output, and concrete examples show how real conditions behave.
| Operator | Symbol forms | Short description |
|---|---|---|
| AND | &&, AND, and | True only when both inputs are true |
| OR | ||, OR, or | True when at least one input is true |
| NOT | !, NOT, not | Flips true to false and false to true |
How and works in logic and programming
The and operator (conjunction) returns true only when every operand is true. In code and queries it is often written as && or AND. For example, in many languages, true && true is true, while true && false is false. Chaining and is associative, so A and B and C is evaluated left to right and is equivalent to (A and B) and C. Short-circuit evaluation means that if the first operand is false, the remaining operands are typically not evaluated, which can affect performance and side effects.
Truth table for and
| A | B | A and B |
|---|---|---|
| false | false | false |
| false | true | false |
| true | false | false |
| true | true | true |
How or works in logic and programming
The or operator (disjunction) returns true when at least one operand is true. It is commonly written as || or OR. For example, true || false is true, and false || false is false. With short-circuit evaluation, if the first operand is true, later operands are usually skipped. This behavior is useful when later checks are costly or could error on invalid inputs.
Truth table for or
| A | B | A or B |
|---|---|---|
| false | false | false |
| false | true | true |
| true | false | true |
| true | true | true |
How not works in logic and programming
The not operator (negation) flips a boolean value. Written as ! or NOT, it is a unary operator that affects a single operand. For example, !true is false, and !false is true. When used with comparisons, such as not (x == y), it inverts the result of the comparison. Short-circuit evaluation is not relevant for not because it has only one operand.
Truth table for not
| A | NOT A |
|---|---|
| false | true |
| true | false |
Using and or not in search and databases
In search engines and database queries, the words AND, OR, NOT and their symbols change which documents match. AND narrows results by requiring multiple terms; OR broadens results by allowing alternatives; NOT excludes terms. Parentheses and explicit grouping are crucial when mixing operators to enforce the intended logic and avoid ambiguous or overly broad result sets.
- Use AND to require multiple conditions.
- Use OR to match any of several conditions.
- Use NOT to exclude conditions.
- Use parentheses to control evaluation order when combining operators.
Precedence, grouping, and common pitfalls
NOT usually has higher precedence than AND, and AND typically binds more strongly than OR. However, exact precedence varies by tool and language. Relying on implicit precedence is risky; explicit parentheses make intent clear and prevent subtle bugs. For example, A OR B AND C can be read differently depending on precedence rules, while (A OR B) AND C or A OR (B AND C) produce distinct logical outcomes.
Worked examples in code and queries
In JavaScript, (x > 0 && ready) || forced shows how mixing && and || works with different precedences. In SQL, SELECT * FROM items WHERE in_stock AND (category = 'books' OR category = 'music') filters rows with explicit grouping. Shell pipelines use && and || to control command execution based on success or failure, and grep -v pattern excludes lines, effectively applying NOT. Each use case demonstrates how understanding the and or not symbols prevents logic errors and unexpected results.
Practical checklist for safe use
- Prefer explicit parentheses when mixing operators.
- Know the short-circuit behavior of your language or tool.
- Remember that NOT applies to the immediately following condition.
- Quote terms and test complex queries in a safe environment.
- Document non-obvious logic for future maintainers.
Common mistakes and how to avoid them
Mistakes often arise from unclear precedence, missing parentheses, or misunderstanding short-circuit evaluation. For example, in A AND B OR C, people may assume OR always runs last. In natural language, and or not can be ambiguous, so in formal queries and code, restate conditions explicitly. Verifying logic with small test cases and truth tables reduces errors and builds confidence.