An if statement lets the program choose to run some code only when a condition is true.
Click to reveal answer
beginner
How do you write an elsif in Ruby?
Use elsif to check another condition if the first if condition is false. It comes after if and before else.
Click to reveal answer
beginner
What does the else part do in an if statement?
else runs code when all previous if and elsif conditions are false.
Click to reveal answer
intermediate
Write a simple Ruby if-elsif-else statement that prints 'Cold', 'Warm', or 'Hot' based on a temperature variable.
Example:
temp = 30
if temp < 10
puts 'Cold'
elsif temp < 25
puts 'Warm'
else
puts 'Hot'
end
Click to reveal answer
intermediate
Can you have multiple elsif branches in one if statement?
Yes, you can have many elsif branches to check several conditions in order.
Click to reveal answer
What will this Ruby code print?
if false
puts 'Yes'
else
puts 'No'
end
ANo
BError
CNothing
DYes
✗ Incorrect
Since the condition is false, the else block runs and prints 'No'.
Which keyword checks a second condition if the first if is false?
Aelse
Belsif
Cwhen
Dcase
✗ Incorrect
elsif lets you check another condition after if.
What happens if none of the if or elsif conditions are true and there is no else?
AThe program crashes
BThe last condition runs anyway
CNo code inside runs
DIt runs the first block
✗ Incorrect
If no conditions match and no else exists, no code inside the statement runs.
How do you end an if statement in Ruby?
Aend
Bendif
Cdone
Dfinish
✗ Incorrect
Ruby uses end to close if statements.
Which is the correct order in Ruby conditional statements?
Aelse, if, elsif
Belsif, if, else
Cif, else, elsif
Dif, elsif, else
✗ Incorrect
The order is if, then zero or more elsif, then optional else.
Explain how if, elsif, and else work together in Ruby.
Think of it like choosing one path out of many based on conditions.
You got /3 concepts.
Write a Ruby example using if, elsif, and else to decide what to wear based on temperature.
Use temperature ranges to decide clothes.
You got /4 concepts.
Practice
(1/5)
1.
What does the elsif keyword do in Ruby?
easy
A. It ends the if statement.
B. It runs code only if all previous conditions were true.
C. It checks another condition if the previous if or elsif was false.
D. It repeats the if block multiple times.
Solution
Step 1: Understand the role of elsif
The elsif keyword is used to check another condition only if the previous if or elsif condition was false.
Step 2: Compare with other options
Options A, C, and D describe behaviors that do not match elsif. It does not end the statement, nor repeats code, nor runs only if all previous conditions were true.
Final Answer:
It checks another condition if the previous if or elsif was false. -> Option C
Quick Check:
elsif = check next condition [OK]
Hint: Remember: elsif checks next condition if previous false [OK]
Common Mistakes:
Thinking elsif ends the if statement
Confusing elsif with else
Assuming elsif runs only if all previous are true
2.
Which of the following is the correct syntax for an if statement with an else in Ruby?
?
easy
A. if x > 10
puts "Big"
else
puts "Small"
end
B. if (x > 10) {
puts "Big"
} else {
puts "Small"
}
C. if x > 10 then puts "Big" else puts "Small"
D. if x > 10:
puts "Big"
else:
puts "Small"
Solution
Step 1: Identify Ruby syntax for if and else
Ruby uses if condition, then code indented or on next line, and ends with end. Curly braces or colons are not used.
Step 2: Check each option
if x > 10
puts "Big"
else
puts "Small"
end matches Ruby syntax exactly. Options A, B, and C use syntax from other languages like colons (A), braces (B), or missing end (C).
Final Answer:
if x > 10
puts "Big"
else
puts "Small"
end -> Option A
score is 75. First condition: score >= 90 is false. Second: score >= 80 is false. Third: score >= 70 is true.
Step 2: Output for first true condition
Since third condition is true, it prints "Grade C" and skips the rest.
Final Answer:
Grade C -> Option D
Quick Check:
75 >= 70 is true, so output is Grade C [OK]
Hint: Check conditions top to bottom; first true runs [OK]
Common Mistakes:
Choosing Grade B or A without checking order
Ignoring that only first true condition runs
Confusing >= with >
4.
Find the error in this Ruby code snippet:
number = 10
if number > 5
puts "Big"
elsif number < 3
puts "Small"
else
puts "Medium"
medium
A. Wrong keyword elsif should be else if.
B. Missing end to close the if statement.
C. The puts statements need parentheses.
D. The comparison operators are incorrect.
Solution
Step 1: Check Ruby syntax completeness
Ruby if statements must end with end. The code snippet is missing end.
Step 2: Verify other parts
elsif is correct in Ruby. puts can be used without parentheses. Comparison operators are correct.
Final Answer:
Missing end to close the if statement. -> Option B
Quick Check:
Every if needs end [OK]
Hint: Always close if with end [OK]
Common Mistakes:
Replacing elsif with else if
Adding unnecessary parentheses to puts
Changing correct comparison operators
5.
You want to write a Ruby program that prints "Positive", "Negative", or "Zero" depending on the value of num. Which code correctly uses if, elsif, and else to do this?
hard
A. if num > 0
puts "Positive"
elsif num < 0
puts "Negative"
else
puts "Zero"
end
B. if num >= 0
puts "Positive"
elsif num <= 0
puts "Negative"
else
puts "Zero"
end
C. if num > 0
puts "Positive"
else if num < 0
puts "Negative"
else
puts "Zero"
end
D. if num > 0
puts "Positive"
elsif num > 0
puts "Negative"
else
puts "Zero"
end
Solution
Step 1: Understand the conditions needed
We want to print "Positive" if num > 0, "Negative" if num < 0, and "Zero" otherwise.
Step 2: Check each option's logic
if num > 0
puts "Positive"
elsif num < 0
puts "Negative"
else
puts "Zero"
end matches the logic exactly with correct if, elsif, and else. if num >= 0
puts "Positive"
elsif num <= 0
puts "Negative"
else
puts "Zero"
end wrongly treats zero as positive. if num > 0
puts "Positive"
else if num < 0
puts "Negative"
else
puts "Zero"
end uses invalid else if syntax in Ruby. if num > 0
puts "Positive"
elsif num > 0
puts "Negative"
else
puts "Zero"
end repeats the same condition twice, which is incorrect.
Final Answer:
if num > 0
puts "Positive"
elsif num < 0
puts "Negative"
else
puts "Zero"
end -> Option A
Quick Check:
Correct conditions and syntax = if num > 0
puts "Positive"
elsif num < 0
puts "Negative"
else
puts "Zero"
end [OK]
Hint: Use elsif for second condition, else for default [OK]
Common Mistakes:
Using else if instead of elsif
Repeating same condition in elsif
Misclassifying zero as positive or negative
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.