Mastering Order of Operations with AND, OR, and NOT Logic

Why Most Developers Get Boolean Logic Wrong

Here's the thing: AND, OR, and NOT seem simple. They're not. Most bugs in conditional logic come from one source—not understanding operator precedence.

You write a condition thinking it evaluates left-to-right. It doesn't. The language follows rules, and if you don't know them, your code does something different than what you intended.

This guide cuts through the confusion. You'll know exactly how these operators work together and in what order.

The Three Operators Explained

AND (&& or AND)

Both sides must be true for the entire expression to be true.

Example: isLoggedIn && hasPermission — returns true only if the user is logged in and has permission.

OR (|| or OR)

At least one side must be true. If both are false, the expression fails.

Example: isAdmin || isModerator — returns true if the user is either an admin or a moderator.

NOT (! or NOT)

Inverts the boolean value. True becomes false. False becomes true.

Example: !isBlocked — returns true if the user is not blocked.

The Actual Order of Operations

Here's the precedence from highest to lowest:

  1. NOT (!) — evaluated first, always
  2. AND (&&) — evaluated second
  3. OR (||) — evaluated last

Most languages follow this order. Some variations exist, but this is the standard.

Why Precedence Matters

Look at this expression:

isAdmin || isLoggedIn && hasAccess

Without parentheses, this evaluates as:

isAdmin || (isLoggedIn && hasAccess)

Not as:

(isAdmin || isLoggedIn) && hasAccess

Big difference. In the first case, being an admin bypasses the need for login and access. In the second case, you need access regardless of admin status.

Operator Precedence Comparison

OperatorSymbolPrecedenceEvaluates
NOT! or NOT1st (highest)Right to left
AND&& or AND2ndLeft to right
OR|| or OR3rd (lowest)Left to right

Real-World Examples

Example 1: Access Control

!isGuest && (hasSubscription || isTrialActive)

Steps:

  1. NOT evaluates first: !isGuest becomes true/false
  2. Parentheses evaluate next: hasSubscription || isTrialActive
  3. Final AND combines both results

Result: Guests are blocked. Paid users and trial users get through.

Example 2: Form Validation

!isEmpty(email) && !isEmpty(password) && isValidEmail(email)

All three conditions must pass. The NOT operators flip the empty checks, so the expression succeeds only when fields are filled.

Example 3: Feature Flags

isEnabled && (isPremium || !isBeta)

The feature works if it's enabled and either the user is premium or it's not a beta feature.

Common Mistakes to Avoid

Getting Started: How to Write Correct Boolean Conditions

Step 1: Identify what must be true

Write down the conditions without operators first. Know what you're checking.

Step 2: Apply NOT where needed

Place your negations next to the conditions they modify.

Step 3: Group related conditions in parentheses

Parentheses override precedence. Use them to enforce your intended logic.

Step 4: Add AND and OR last

Connect your grouped conditions with the broader operators.

Step 5: Test edge cases

Check all combinations: both true, both false, one true one false.

Quick Reference

true && true = true
true && false = false
false && false = false

true || false = true
false || false = false
true || true = true

!true = false
!false = true

When mixing operators, remember: NOT beats AND beats OR. Parentheses always win.