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.
if StatementThe 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")
}
if...else StatementUse 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")
}
ifelse() Function (Vectorized)ifelse() is a vectorized version of if...else. It evaluates a condition for each element in a vector.
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"
else if LadderUse 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")
}
for LoopRepeats 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)
}
while LoopRepeats 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
}
repeat LoopRepeats code indefinitely until a break statement stops it.
repeat {
# code
if (condition) {
break # exit the loop
}
}
Example:
num <- 1
repeat {
print(num)
num <- num + 1
if (num > 5) {
break
}
}
break and next Statementsbreak exits the loop immediately.next skips the current iteration and moves to the next loop cycle.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
| 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 } |
{ } for code blocks, even if they contain one statement — it improves readability.ifelse() for vectorized conditional operations to write concise and efficient code.repeat or while loops have a terminating condition.break and next help you control loop flow smartly — use them wisely.Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!