01 / 20

If/Else & Else-If
Chains in R

Learn to control the flow of your R programs using conditional logic: if, else, and else if statements.

R Programming

What Is Conditional Logic?

Every program needs to make decisions. Should we print a greeting or a warning? Should we calculate a discount or charge full price? Conditional statements let your code choose which path to take based on whether a condition is TRUE or FALSE.

The if Statement

The simplest conditional: execute a block of code only when a condition is TRUE. If it's FALSE, the block is skipped entirely.

if (condition) { # code runs only when condition is TRUE }

How if Flows

Start condition TRUE / FALSE? TRUE Run code FALSE Continue

Adding else: Two Paths

When you need two possible outcomes, pair if with else. The else block executes whenever the condition is FALSE: you always get exactly one of the two paths.

if (condition) { # runs when TRUE } else { # runs when FALSE }

if/else Flowchart

Start condition? TRUE if block FALSE else block Continue

The else if Chain

When you have three or more possible outcomes, use an else if chain. R checks each condition in order and executes the first one that is TRUE.

if (condition1) { # runs when condition1 is TRUE } else if (condition2) { # runs when condition2 is TRUE } else if (condition3) { # runs when condition3 is TRUE } else { # runs when ALL above are FALSE }

if vs else if vs Nested if

Separate if
Each if is independent. Multiple blocks can run if multiple conditions are TRUE.
else if Chain
Mutually exclusive. Only the first matching block runs. Once a condition is TRUE, the rest are skipped.
Nested if
An if inside another if. Checks a sub-condition only when the outer condition is already TRUE.

How to Write a Conditional

1
Identify the decision. What question does your program need to answer?
2
Count the outcomes. Two outcomes: use if/else. Three or more: use an else if chain.
3
Write conditions. Each condition must return a single TRUE or FALSE. Use comparison and logical operators.
4
Order matters. Put the most specific condition first. R stops at the first TRUE.
5
Add a final else. Always include a fallback to handle unexpected inputs.

Example 1: Pass or Fail

score <- 72 if (score >= 60) { print("Pass") } else { print("Fail") }

Example 2: Letter Grade

score <- 85 if (score >= 90) { grade <- "A" } else if (score >= 80) { grade <- "B" } else if (score >= 70) { grade <- "C" } else { grade <- "F" }

Why Order Matters

Watch what happens if we reverse the order and check the least specific condition first:

# BAD ORDER: least specific first! if (score >= 70) { grade <- "C" # score=85 matches here! } else if (score >= 80) { grade <- "B" # never reached for 85 } else if (score >= 90) { grade <- "A" # never reached for 85 }

Example 3: Nested Conditions

age <- 20 has_id <- TRUE if (age >= 18) { if (has_id) { print("Entry allowed") } else { print("Need ID") } } else { print("Too young") }

Logical Operators in Conditions

You can combine multiple conditions into a single test using logical operators:

&& (AND)
TRUE only when both sides are TRUE.
|| (OR)
TRUE when at least one side is TRUE.
! (NOT)
Flips TRUE to FALSE and FALSE to TRUE.
# Combine the nested example into one line: if (age >= 18 && has_id) { print("Entry allowed") }

Common Mistakes

✕ else on New Line
Writing else on a new line after } causes an error. Keep } else { on the same line.
✕ Using = Instead of ==
Single = is assignment. Use == for comparison. if (x = 5) assigns, not compares.
✕ Wrong Condition Order
Putting broad conditions first in an else if chain means specific conditions are never reached.

Summary Comparison

Structure Blocks That Run Use When
if Single condition 0 or 1 One optional action
if/else Two branches Exactly 1 Binary decision
else if chain Multiple branches Exactly 1 Mutually exclusive outcomes
Nested if if inside if Varies Sub-conditions
Separate ifs Independent blocks 0 to all Independent checks

Question 1 of 5

What does the following code print?

x <- 15
if (x > 20) { print("A") } else { print("B") }

Question 2 of 5

In an else if chain, how many code blocks execute when a condition is TRUE?

Question 3 of 5

What is the value of grade after this code runs?

score <- 95
if (score >= 90) { grade <- "A" }
else if (score >= 80) { grade <- "B" }
else { grade <- "C" }

Question 4 of 5

Which line causes an error in R?

Question 5 of 5

What is the difference between using two separate if statements versus an if/else if chain?