Control Flow - If Else Statement
Control Flow - If Else Statement
Control Flow is a critical concept in programming that allows for the execution of code in a non-linear
fashion.
Control flow structures in R allow you to specify which pieces of code are to be executed and in what
order. These structures enable you to create dynamic programs that can make decisions, repeat
actions, and handle different input scenarios.
'if' Statement
The if statement is a fundamental component of flow control in R. It evaluates a given condition, and
if the condition is TRUE , the block of code within the if statement is executed. If the condition is
FALSE , the if statement is ignored, and the program continues to the next line of code outside the
if block.
if (condition) {
# code to be executed if the condition is TRUE
}
x <- 5
if (x > 0) {
print("x is positive")
}
if else statement
The else clause can be added to execute code when the condition is FALSE .
x <- -3
if (x > 0) {
print("x is positive")
} else {
print("x is not positive")
}
age <- 20
if (age >= 18) {
print("You are eligible to vote.")
}
number <- -5
if (number >= 0) {
print(paste(number, "is a non-negative number."))
} else {
print(paste(number, "is a negative number."))
}
x <- 0
if (x > 0) {
print("x is positive")
} else if (x < 0) {
print("x is negative")
} else {
print("x is zero")
}
score <- 85
age <- 25
num <- 7
if (num < 0) {
print("Negative")
if (num %% 2 == 0) {
print("Even")
} else {
print("Odd")
}
} else if (num == 0) {
print("Zero")
} else {
print("Positive")
if (num %% 2 == 0) {
print("Even")
[1] "Positive"
[1] "Odd"
[1] "A" "B" "C" "B" "D" "A" "C" "B" "B" "F"
# Initialize counters
count_positive <- 0
count_negative <- 0
count_zero <- 0
# Print results
cat("Positive numbers:", count_positive, "\n")
Positive numbers: 4
Negative numbers: 3
Zeros: 3
# Sample ages
ages <- c(25, 42, 15, 33, 68, 72, 19, 56, 9, 31)
ifelse statement
The ifelse() function in R is a vectorized alternative to the if-else statement, allowing for more
concise and efficient conditional operations on vectors. It evaluates a condition for each element of a
vector and returns a new vector with values assigned based on whether the condition is TRUE or
FALSE .
ifelse(test_expression, x, y)
print(categories)
Exercise
Given a vector of temperatures in Celsius, classify each temperature as “Cold” if it’s below 10°C,
“Moderate” if it’s between 10°C and 25°C, and “Hot” if it’s above 25°C. Store the results in a new
vector and print it.
You have a vector containing single characters. Write a program to count the number of occurrences
of vowels (a, e, i, o, u) in the vector.
chars <- c('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'o', 'p', 'u')
Given a vector of student scores, determine how many students passed and how many failed. Assume
a passing score is 50 or above.
scores <- c(45, 56, 67, 48, 90, 75, 33, 52)
You have a vector containing heights of various individuals in centimeters. Categorize each height as
“Short” if below 160cm, “Average” if between 160cm and 180cm, and “Tall” if above 180cm. Store the
heights <- c(155, 162, 171, 185, 150, 178, 182, 165, 170)
You have a vector containing the prices of items in a store. Any item that costs more than $100 gets a
10% discount, while items costing $50 or less get a 5% discount. Adjust the prices according to these
criteria using the ifelse() function.
Given a vector of student scores, provide remarks based on the following criteria:
60 to 74: “Good”
50 to 59: “Average”
Use the ifelse() function to categorize the student scores into the respective remarks.
You have a vector containing daily average temperatures. Classify each day’s weather as: