Decision Making in R Programming if, ifelse, ifelseif ladder, nested ifelse, and switch (original) (raw)
Last Updated : 07 Jun, 2025
Decision making in programming allows us to control the flow of execution based on specific conditions. In R, various decision-making structures help us execute statements conditionally. These include:
- **if statement
- **if-else statement
- **if-else-if ladder
- **nested if-else statement
- **switch statement
1. if Statement
The if statement evaluates a condition. If the condition is TRUE, the associated statement is executed. If the condition is FALSE, the statement is skipped.
**Syntax:
if (condition) {
# execute this statement
}
**Flow Chart:
**Example:
R `
a <- 76 b <- 67
if (a > b) { c <- a - b print("condition a > b is TRUE") print(paste("Difference between a, b is:", c)) }
if (a < b) { c <- a - b print("condition a < b is TRUE") print(paste("Difference between a, b is:", c)) }
`
**Output:
[1] "condition a > b is TRUE"
[1] "Difference between a, b is: 9"
2. if-else Statement
The **if-else statement executes one block if the condition is **TRUE and another if it is **FALSE.
**Syntax:
if (condition) {
# execute this statement
} else {
# execute this statement
}
**Flow Chart:
**Example :
r `
a <- 67 b <- 76
if (a > b) { c <- a - b print("condition a > b is TRUE") print(paste("Difference between a, b is:", c)) } else { c <- a - b print("condition a > b is FALSE") print(paste("Difference between a, b is:", c)) }
`
**Output:
[1] "condition a > b is FALSE"
[1] "Difference between a, b is : -9"
3. if-else-if Ladder
This structure chains multiple conditions together. Each condition is evaluated in sequence. If a condition is TRUE, its block is executed. Otherwise, the next condition is checked.
**Syntax:
if (condition1) {
# execute this statement
} else if (condition2) {
# execute this statement
} else {
# execute this statement
}
**Flow Chart:
**Example :
r `
a <- 67 b <- 76 c <- 99
if (a > b && b > c) { print("condition a > b > c is TRUE") } else if (a < b && b > c) { print("condition a < b > c is TRUE") } else if (a < b && b < c) { print("condition a < b < c is TRUE") }
`
**Output:
[1] "condition a < b < c is TRUE"
4. Nested if-else Statement
Nested if-else statements are used when one condition needs to be checked inside another. If the parent condition is TRUE, the child condition is evaluated.
**Syntax:
if (parent_condition) {
if (child_condition1) {
# execute this statement
} else {
# execute this statement
}
} else {
if (child_condition2) {
# execute this statement
} else {
# execute this statement
}
}
**Flow Chart:
**Example:
r `
a <- 10 b <- 11
if (a == 10) { if (b == 10) { print("a:10 b:10") } else { print("a:10 b:11") } } else { if (a == 11) { print("a:11 b:10") } else { print("a:11 b:11") } }
`
**Output:
[1] "a:10 b:11"
5. switch Statement
The switch statement compares an expression to a list of possible cases. If a match is found, the corresponding case is executed. If no match is found, it returns NULL.
**Syntax:
switch(expression, case1, case2, case3, ..., caseN)
**Flow Chart :
**Example:
r `
x <- switch(2, "Geeks1", "for", "Geeks2") print(x)
y <- switch("GfG3", "GfG0"="Geeks1", "GfG1"="for", "GfG3"="Geeks2") print(y)
z <- switch("GfG", "GfG0"="Geeks1", "GfG1"="for", "GfG3"="Geeks2") print(z)
`
**Output:
[1] "for"
[1] "Geeks2"
NULL