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") }
✗ Incorrect
Since 5 is greater than 3, the condition is TRUE, so it prints "Yes".
Which keyword is used to check an additional condition after an if in R?
✗ Incorrect
In R, the correct syntax is 'else if' with a space, not 'elseif' or 'elif'.
What happens if the condition in an if statement is FALSE and there is no else?
✗ Incorrect
If the condition is FALSE and no else is present, the if block is skipped.
Which of these is the correct syntax for an if-else statement in R?
✗ Incorrect
Option D uses correct braces and else block syntax in R.
How do you write multiple conditions in R to check if x is positive, zero, or negative?
✗ Incorrect
Option C uses the correct 'else if' syntax with parentheses.
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.