Looping Statement (Printout)
Looping Statement (Printout)
} } else {
print("You failed.")
❖ IF ELSE STATEMENT (3 EXAMPLES)
}
1. Categorizing age groups.
age <- 25 2. Check for voting age.
age <- 20
if (age < 13) {
if (age < 13) {
category <- "Child"
print("You are a Child.")
} else if (age >= 13 & age <= 19) {
} else if (age >= 13 & age <= 19) {
category <- "Teenager"
print("You are a Teenager.")
} else if (age >= 20 & age <= 59) {
} else {
category <- "Adult"
if (age >= 18) {
} else {
print("You are an Adult and eligible to vote.")
category <- "Senior"
} else {
}
print("You are not eligible to vote yet.")
print(paste("Age group:", category))
}
2. Checking if number is PRIME.
num <- 11 }
print("You receive a 10% discount.") ifelse(age >= 20 & age <= 59, "Adult",
} else {
print("No discount available.")
}
3. BMI calculation.
weight <- 70
height <- 1.75
II. SWITCH() STATEMENTS )
"IV" = 4, n <- n - 1
"V" = 5, }
cat("The integer value is:", integer_value) num <- scan(what = numeric(), nmax = 1)
3. Time of the day. factorial <- 1
cat("Enter the time of day (0 to 23): ")
count <- 1
time_of_day <- scan(what = numeric(), nmax = 1)
if (i %% 2 == 0) { cat("Original Matrix:\n")
} sum_elements <- 0
} for (i in 1:4) {