01 / 21

Conditionals
Decision Making in Code

Learn how programs make decisions using Boolean expressions, if, else, and nested conditionals to control the flow of execution.

AP Computer Science Principles

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.

Relational
\( = \), \( \neq \), \( < \), \( > \), \( \leq \), \( \geq \) compare two values
Logical
AND, OR, NOT combine or negate Boolean values
Result
Always produces exactly true or false: nothing else

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.

1
User interaction: Show different content based on what a user clicks or types.
2
Data validation: Check if an input is valid before processing it.
3
Game logic: Determine if a player wins, loses, or levels up.
4
Error handling: Prevent crashes by checking for edge cases.

The IF Statement

The simplest conditional: if the condition is true, execute the code block. If false, skip it entirely and continue.

START condition? TRUE Code FALSE 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.

START condition? TRUE Block A FALSE Block B CONTINUE

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.

IF (condition1) { // runs if condition1 is true } ELSE IF (condition2) { // runs if condition1 is false AND condition2 is true } ELSE { // runs if both conditions are false }

How to Apply Conditionals

1
Identify the decision: What question does your program need to answer?
2
Write the Boolean expression: Translate the question into a condition that evaluates to true or false.
3
Choose the structure: Use if for optional actions, if-else for two paths, or if / else if / else for multiple paths.
4
Write each code block: Define what happens for each branch.
5
Trace with test values: Mentally walk through the code with different inputs to verify correctness.

Example 1: Simple IF

A weather app displays a warning only when the temperature exceeds 100. Let's trace with temp = 105.

temp ← 105 IF (temp > 100) { DISPLAY("Heat warning!") }

Example 2: IF-ELSE

A grading program classifies a score as passing or failing. Trace with score = 72.

score ← 72 IF (score ≥ 65) { DISPLAY("Pass") } ELSE { DISPLAY("Fail") }

Example 3: Chained Conditionals

A letter-grade calculator with multiple thresholds. Trace with score = 83.

score ← 83 IF (score ≥ 90) { grade ← "A" } ELSE IF (score ≥ 80) { grade ← "B" } ELSE IF (score ≥ 70) { grade ← "C" } ELSE { grade ← "F" }

Logical Operators in Conditions

You can combine multiple Boolean expressions into a single condition using AND, OR, and NOT.

AND
true only when both sides are true.
\( \text{age} \ge 16 \) AND \( \text{hasPermit} \)
OR
true when at least one side is true.
\( \text{isMember} \) OR \( \text{hasTicket} \)
NOT
Flips the value. NOT true = false. NOT false = true.
IF (age ≥ 16 AND hasPermit = true) { DISPLAY("Can drive") }

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.

IF (height ≥ 48) { IF (age ≥ 8) { DISPLAY("Ride approved") } ELSE { DISPLAY("Too young") } } ELSE { DISPLAY("Too short") }

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.

Test height ≥ 48 first. A tall rider is allowed without testing consent. A shorter rider needs consent for supervised riding; otherwise riding is denied. The consent value has already been input.
Test height ≥ 48 first. A tall rider is allowed without testing consent. A shorter rider needs consent for supervised riding; otherwise riding is denied. The consent value has already been input.

Common Mistakes

✕ Wrong operator
Confusing comparison (\(=\)) with assignment (\(\leftarrow\)). A condition checks if something is true (e.g., \( \text{score} = 100 \)), while assignment changes a variable's value (e.g., \( \text{score} \leftarrow 100 \)).
✕ Overlapping conditions
Checking \( \text{score} \ge 80 \) before \( \text{score} \ge 90 \) in a chain: a 95 matches the first branch and gets "B" instead of "A".
✕ Missing else
Using multiple independent if statements instead of if-else if, causing multiple blocks to run.

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

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

What value of grade is displayed?

Question 4 of 5

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?