R Tutorial



R CONTROL FLOW


Control Flow in R

Control flow statements allow your R programs to make decisions, repeat tasks, or choose among alternatives based on conditions. Mastering control flow helps you write dynamic, flexible programs.

1. if Statement

The if statement executes a block of code only if a specified condition is TRUE.

if (condition) {
  # code to run if condition is TRUE
}
  
Example:
x <- 10
if (x > 5) {
  print("x is greater than 5")
}
  

2. if...else Statement

Use if...else to run one block of code when the condition is TRUE, and another when it is FALSE.

if (condition) {
  # if TRUE, execute this
} else {
  # if FALSE, execute this
}
  
Example:
age <- 18
if (age >= 18) {
  print("You are an adult")
} else {
  print("You are a minor")
}
  

3. ifelse() Function (Vectorized)

ifelse() is a vectorized version of if...else. It evaluates a condition for each element in a vector.

Syntax:
ifelse(condition, value_if_true, value_if_false)
  
Example:
scores <- c(75, 42, 89, 55)
result <- ifelse(scores >= 50, "Pass", "Fail")
print(result)  # "Pass" "Fail" "Pass" "Pass"
  

4. else if Ladder

Use else if to test multiple conditions sequentially.

if (condition1) {
  # code if condition1 TRUE
} else if (condition2) {
  # code if condition2 TRUE
} else {
  # code if none above TRUE
}
  
Example:
marks <- 85
if (marks >= 90) {
  print("Grade: A")
} else if (marks >= 75) {
  print("Grade: B")
} else if (marks >= 50) {
  print("Grade: C")
} else {
  print("Grade: F")
}
  

5. for Loop

Repeats code for each element in a sequence or vector.

Syntax:
for (variable in sequence) {
  # code to execute repeatedly
}
  
Example:
for (i in 1:5) {
  print(i * 2)
}
  

6. while Loop

Repeats code as long as a condition is TRUE.

Syntax:
while (condition) {
  # code to repeat
}
  
Example:
count <- 1
while (count <= 5) {
  print(count)
  count <- count + 1
}
  

7. repeat Loop

Repeats code indefinitely until a break statement stops it.

Syntax:
repeat {
  # code
  if (condition) {
    break  # exit the loop
  }
}
  
Example:
num <- 1
repeat {
  print(num)
  num <- num + 1
  if (num > 5) {
    break
  }
}
  

8. break and next Statements

  • break exits the loop immediately.
  • next skips the current iteration and moves to the next loop cycle.
Example using next and break:
for (i in 1:10) {
  if (i == 3) {
    next   # skip 3
  }
  if (i == 7) {
    break  # stop loop at 7
  }
  print(i)
}
# Output: 1 2 4 5 6
  

Summary Table

Control Flow Purpose Example
if Execute code if condition is TRUE if (x > 5) { ... }
if...else Choose between two options if (cond) { ... } else { ... }
ifelse() Vectorized if...else for vectors ifelse(x > 5, "Yes", "No")
for Repeat code for each element for (i in 1:5) { ... }
while Repeat while condition TRUE while (x < 10) { ... }
repeat Infinite loop until break repeat { ... if (cond) break }

Tips for Mastery

  • Always use braces { } for code blocks, even if they contain one statement — it improves readability.
  • Use ifelse() for vectorized conditional operations to write concise and efficient code.
  • Be careful with infinite loops; always ensure your repeat or while loops have a terminating condition.
  • break and next help you control loop flow smartly — use them wisely.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review