0
0
R Programmingprogramming~5 mins

If-else statements in R Programming - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if-else statement in R?
An if-else statement lets the program choose between two paths based on a condition. If the condition is true, it runs one block of code; if false, it runs another.
Click to reveal answer
beginner
How do you write a simple if statement in R?
Use the syntax: if (condition) { code } where 'condition' is a test that returns TRUE or FALSE, and 'code' runs only if the condition is TRUE.
Click to reveal answer
beginner
What happens if the condition in an if statement is FALSE and there is no else?
The code inside the if block is skipped, and the program continues with the next lines after the if statement.
Click to reveal answer
beginner
How do you write an if-else statement in R?
Use: if (condition) { code if TRUE } else { code if FALSE }. This runs one block if the condition is TRUE, otherwise the other block.
Click to reveal answer
intermediate
Can you have multiple conditions checked in R using if-else?
Yes, by using else if between if and else, you can check multiple conditions in order.
Click to reveal answer
What will this R code print? if (5 > 3) { print("Yes") } else { print("No") }
AYes
BNo
C5
DError
Which keyword is used to check an additional condition after an if in R?
Aelseif
Belse if
Celif
Delseif()
What happens if the condition in an if statement is FALSE and there is no else?
AThe code inside if is skipped
BThe code inside if runs anyway
CThe program crashes
DThe else block runs
Which of these is the correct syntax for an if-else statement in R?
Aif x > 0 then print("Positive") else print("Non-positive")
Bif (x > 0) { print("Positive") } else print("Non-positive")
Cif (x > 0) print("Positive") else print("Non-positive")
Dif (x > 0) { print("Positive") } else { print("Non-positive") }
How do you write multiple conditions in R to check if x is positive, zero, or negative?
Aif (x > 0) {...} else if x == 0 {...} else {...}
Bif (x > 0) {...} elseif (x == 0) {...} else {...}
Cif (x > 0) {...} else if (x == 0) {...} else {...}
Dif (x > 0) {...} else (x == 0) {...} else {...}
Explain how an if-else statement controls the flow of a program in R.
Think about how the program chooses which code to run based on a test.
You got /4 concepts.
    Write an example of an if-else statement in R that prints "Even" if a number is even, and "Odd" if it is odd.
    Use the modulo operator %% to check if a number is divisible by 2.
    You got /4 concepts.