0
0
Rubyprogramming~5 mins

If, elsif, else statements in Ruby - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of an if statement in Ruby?
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?<br>
if false
  puts 'Yes'
else
  puts 'No'
end
ANo
BError
CNothing
DYes
Which keyword checks a second condition if the first if is false?
Aelse
Belsif
Cwhen
Dcase
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
How do you end an if statement in Ruby?
Aend
Bendif
Cdone
Dfinish
Which is the correct order in Ruby conditional statements?
Aelse, if, elsif
Belsif, if, else
Cif, else, elsif
Dif, elsif, 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.