Control Flow

Control flow is how R decides what to run and how often — branching with if/else and repeating with for and while loops, so your program can react to data instead of running straight through.

Learn Control Flow in our free R course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.

Part of the free R course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

By the end of this lesson you'll branch with if/else if/else, transform vectors with ifelse(), repeat work with for and while loops, and steer loops with break and next.

What You'll Learn in This Lesson

1️⃣ Branching with if / else

An if statement runs a block only when its condition is TRUE; else if and else add fallbacks. For deciding values across a whole vector, use the vectorized ifelse() instead.

2️⃣ Repeating with for Loops

A for loop runs its body once for each element of a vector. You can loop over indices ( 1:3 ) or over the items themselves. seq_along() gives safe indices that won't break on empty vectors.

3️⃣ while Loops, break, and next

A while loop repeats as long as its condition holds — make sure something inside changes the condition, or it never stops. break exits a loop early; next skips to the next iteration.

Your turn. Fill in the # TODO blank, run it, and compare with the expected output.

The classic interview warm-up. Write it from the outline, run it, and check it against the example output. It exercises loops, %% , and nested conditions.

📋 Quick Reference — Control Flow

Practice quiz

How many values must the condition in an if statement evaluate to?

  • A whole vector of conditions
  • At least two
  • Exactly one (a single TRUE or FALSE)
  • Zero

Answer: Exactly one (a single TRUE or FALSE). if needs a single logical value; a length>1 vector causes an error.

What does ifelse(c(45, 80) >= 60, "pass", "fail") return?

  • c("fail", "pass")
  • c("pass", "fail")
  • A single value "fail"
  • An error

Answer: c("fail", "pass"). ifelse() works element by element: 45 fails, 80 passes.

Which loop runs its body once for each element of a vector?

  • repeat-until
  • do-while
  • switch
  • for

Answer: for. for (x in v) iterates over each element of v.

Why prefer seq_along(x) over 1:length(x) in a loop?

  • seq_along is shorter to type only
  • 1:length(x) misbehaves when x is empty (becomes 1:0)
  • 1:length(x) is a syntax error
  • seq_along runs in parallel

Answer: 1:length(x) misbehaves when x is empty (becomes 1:0). For empty x, 1:length(x) is 1:0 and iterates twice; seq_along gives nothing.

A while loop keeps running as long as its condition is...

  • TRUE
  • FALSE
  • NA
  • numeric

Answer: TRUE. while repeats while the condition stays TRUE.

What does break do inside a loop?

  • Skips to the next iteration
  • Restarts the loop
  • Exits the entire loop immediately
  • Pauses for one second

Answer: Exits the entire loop immediately. break leaves the loop entirely; next skips to the next iteration.

What does next do inside a loop?

  • Exits the loop completely
  • Skips the rest of the current iteration
  • Doubles the counter
  • Prints the index

Answer: Skips the rest of the current iteration. next jumps straight to the next iteration without finishing the body.

What does the expression 7 %% 3 evaluate to?

  • 2
  • 3
  • 0
  • 1

Answer: 1. %% is the modulo operator; 7 divided by 3 leaves remainder 1.

A common cause of an infinite while loop is...

  • Using break too early
  • Forgetting to update the variable the condition checks
  • Looping over a vector
  • Calling cat() inside it

Answer: Forgetting to update the variable the condition checks. If nothing makes the condition FALSE, the loop never ends.

Which is the VECTORIZED way to choose values per element?

  • if (cond) a else b
  • for with break
  • ifelse(cond, a, b)
  • while (cond)

Answer: ifelse(cond, a, b). ifelse() is vectorized; if handles a single condition for control flow.