if / elsif / else
Try it yourself →Executes code based on a condition.
Syntax
if condition ... elsif condition ... else ... end
Description
Ruby uses end for blocks (not curly braces). Indentation is not mandatory but recommended for readability.
Example
score = 75 if score >= 85 puts "Excellent" elsif score >= 50 puts "Passed" else puts "Failed" end
Expected output
Passed
When to use?
Flow branching.
Common mistakes
- ⚠️ Don't forget to write end — every if/def/loop closes with an end.