Conditionals
Decision Making in Code
Learn how programs make decisions using Boolean expressions, if, else, and nested conditionals to control the flow of execution.
What Are Conditionals?
A conditional is a programming construct that executes different blocks of code depending on whether a Boolean condition evaluates to true or false. Conditionals let your program make decisions instead of running every line in a fixed order.
Boolean Expressions
Every conditional relies on a Boolean expression: a statement that evaluates to exactly one of two values: true or false. These are built using relational and logical operators.
Why Do Conditionals Matter?
Without conditionals, a program would execute the same instructions every time regardless of input. Conditionals give software the ability to respond to different situations: validating passwords, granting permissions, handling errors, and more.
The IF Statement
The simplest conditional: if the condition is true, execute the code block. If false, skip it entirely and continue.
The IF-ELSE Statement
An if-else provides two paths: one block runs when the condition is true, and a different block runs when it is false. Exactly one of the two blocks always executes.
IF vs. IF-ELSE
| IF Only | IF-ELSE | |
|---|---|---|
| Paths | 1 (execute or skip) | 2 (one always runs) |
| When false | Nothing happens | Else block executes |
| Guaranteed execution | No | Yes: exactly one block |
| Use case | Optional action | Choosing between two actions |
Nested and Chained Conditionals
When you need more than two paths, you can chain conditionals (if / else if / else) or nest one conditional inside another. Chaining checks conditions in order and runs the first matching block.
How to Apply Conditionals
Example 1: Simple IF
A weather app displays a warning only when the temperature exceeds 100. Let's trace with temp = 105.
Example 2: IF-ELSE
A grading program classifies a score as passing or failing. Trace with score = 72.
Example 3: Chained Conditionals
A letter-grade calculator with multiple thresholds. Trace with score = 83.
Logical Operators in Conditions
You can combine multiple Boolean expressions into a single condition using AND, OR, and NOT.
\( \text{age} \ge 16 \) AND \( \text{hasPermit} \)
\( \text{isMember} \) OR \( \text{hasTicket} \)
Example 4: Nested Conditionals
A theme park ride requires riders to be at least 48 inches tall AND at least 8 years old. Trace with height = 50, age = 7.
Separate height and consent example
This separate example uses a consent condition rather than the age condition in the deck’s ride problem. Test height ≥ 48 first; the consent value is already input.

Common Mistakes
Conditionals Summary
| Structure | Behavior | |
|---|---|---|
| IF | 1 condition, 1 block | Block runs or is skipped |
| IF-ELSE | 1 condition, 2 blocks | Exactly one block always runs |
| IF / ELSE IF / ELSE | Multiple conditions | First true branch runs; rest skipped |
| Nested IF | IF inside another IF | Inner checked only if outer is true |
| Boolean operators | AND, OR, NOT | Combine/negate conditions |
Question 1 of 5
Consider the following code:
x ← 15
IF (x > 20)
{
DISPLAY("big")
}
ELSE
{
DISPLAY("small")
}
What is displayed when this code runs?
Question 2 of 5
Which Boolean expression is true when \( \text{age} = 20 \) and \( \text{hasID} = \text{false} \)?
Question 3 of 5
Consider the following code:
score ← 85
IF (score ≥ 90)
{
grade ← "A"
}
IF (score ≥ 80)
{
grade ← "B"
}
IF (score ≥ 70)
{
grade ← "C"
}
DISPLAY(grade)
What value of grade is displayed?
Question 4 of 5
Consider the following code:
x ← 10
y ← 5
IF (x > 5)
{
IF (y > 8)
{
DISPLAY("A")
}
ELSE
{
DISPLAY("B")
}
}
ELSE
{
DISPLAY("C")
}
What is displayed?
Question 5 of 5
A programmer wants a program to display "Eligible" only when a user is at least 18 years old AND has completed training. Which code is correct?