A conditional statement runs one block of code when a condition is true and a different block (or nothing at all) when it is false. The condition is always a boolean expression — something that evaluates to exactly true or false — so writing reliable conditionals comes down to writing reliable boolean logic.

If you only need the core idea, it is this: every if/else decision reduces to evaluating a single boolean expression, and a truth table lets you check that expression case by case before you trust it.

How if, else if, and else work

An if statement evaluates its condition first. If the condition is true, its block runs and every later branch in the same chain is skipped. If it is false, control moves to the next else if (written elif in Python), and finally to else, which has no condition and catches everything that remains.

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
else:
    grade = "C"

Three rules govern every chain like this:

  1. Conditions are tested top to bottom.
  2. Only the first true branch runs — the rest are skipped, even if their conditions are also true.
  3. The else branch runs only when every condition above it was false.

Boolean logic inside conditions

Compound conditions are built from three operators. In logic notation, a condition is a proposition pp or qq that is either true or false.

Operator Logic symbol True when... Code example
AND pqp \land q both sides are true age >= 18 and has_id
OR pqp \lor q at least one side is true is_member or total >= 50
NOT ¬p\lnot p the operand is false not is_banned

AND is strict, OR is permissive, and NOT flips a value. Most real-world bugs in conditionals come from combining these three incorrectly, which is exactly what truth tables are designed to catch.

Worked example 1: tracing an if/else chain

Take the grading chain above with score = 84.

  1. Test score >= 9084 >= 90 is false, so skip the "A" branch.
  2. Test score >= 8084 >= 80 is true, so grade = "B".
  3. The else branch is skipped entirely because a branch already ran.

Now notice why order matters. If you wrote the score >= 80 test first, a score of 95 would satisfy it, assign "B", and never reach the >= 90 test. The chain works only because branches are ordered from most specific to most general.

Truth tables: checking a compound condition

A truth table lists every combination of inputs and the resulting value of the whole expression. With nn boolean inputs there are 2n2^n rows.

Suppose an online store gives free shipping when is_member or total >= 50. Let pp be "is a member" and qq be "total is at least 50":

pp qq pqp \lor q Free shipping?
T T T yes
T F T yes
F T T yes
F F F no

The table makes the behavior explicit: only a non-member with a small order pays for shipping. Four rows, no ambiguity.

Worked example 2: verifying De Morgan's laws

Negating compound conditions is where most logic errors happen. Suppose you want the opposite of the free-shipping rule — the customers who must pay. The tempting rewrite not is_member and total >= 50 is wrong. De Morgan's laws give the correct transformation:

¬(pq)=¬p¬q\lnot(p \lor q) = \lnot p \land \lnot q

So not (is_member or total >= 50) is equivalent to not is_member and total < 50. Verify it with a truth table:

pp qq ¬(pq)\lnot(p \lor q) ¬p¬q\lnot p \land \lnot q
T T F F
T F F F
F T F F
F F T T

The last two columns match on every row, so the two expressions are logically equivalent. When two columns match for all 2n2^n rows, you can substitute one expression for the other anywhere in your code.

Common conditional mistakes

Using assignment instead of comparison

In many languages = assigns and == compares. Writing if (x = 5) stores 5 in x and then evaluates the result, which is often truthy — so the branch always runs. Always double-check the operator inside a condition.

Ordering branches from general to specific

Putting score >= 80 before score >= 90 silently swallows the A students. In any elif chain, the most restrictive condition must come first.

Distributing NOT without flipping the operator

¬(pq)\lnot(p \land q) is not ¬p¬q\lnot p \land \lnot q — De Morgan's laws require the AND to become an OR. If a negated condition behaves strangely, build its four-row truth table and compare.

Comparing a boolean to true

if is_valid == True is redundant and, in loosely typed languages, can introduce coercion surprises. Write if is_valid directly.

Where conditionals show up

Conditionals are the branching mechanism behind almost everything: input validation (reject an empty form field), guard clauses at the top of functions, game logic (collision detected → reduce health), and core algorithms — binary search, for example, is a loop around a single conditional that decides which half of the array to keep.

Check yourself with a classic

A year is a leap year when it is divisible by 4, except century years, which must be divisible by 400. As a boolean expression with pp = "divisible by 4", qq = "divisible by 100", rr = "divisible by 400":

p(¬qr)p \land (\lnot q \lor r)

Build the truth table for 1900 (pp T, qq T, rr F) and 2000 (pp T, qq T, rr T). If your table says 1900 is not a leap year but 2000 is, your boolean logic is in good shape.

Frequently Asked Questions

What is the difference between else if and else?
An else if branch has its own condition and runs only when that condition is true and every earlier condition was false. An else branch has no condition at all: it is the catch-all that runs when every condition in the chain failed. A chain can have many else if branches but at most one else, and it must come last.
What is a truth table used for in programming?
A truth table lists every possible combination of true and false inputs for a boolean expression and shows the result for each row. Programmers use it to verify that a compound condition behaves as intended, to prove two expressions are equivalent, and to catch negation errors before they become bugs. With n inputs there are 2 to the power n rows.
What is the difference between AND and OR in a condition?
AND is true only when both sides are true, so it makes a condition stricter. OR is true when at least one side is true, so it makes a condition easier to satisfy. Mixing them up inverts the behavior of a branch, which is why checking a compound condition against a small truth table is a reliable habit.
Why does the order of conditions matter in an if else chain?
Because only the first true branch runs. If a general condition like score at least 80 appears before a stricter one like score at least 90, every high score is captured by the general branch and the stricter branch can never run. Order branches from most specific to most general to avoid this.

Need help with a problem?

Upload your question and get a verified, step-by-step solution in seconds.

Open GPAI Solver →