if / elsif / else
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
Loading editor…
Expected output
Passed
When to use?
Flow branching.
Common mistakes
- ⚠️ Don't forget to write end — every if/def/loop closes with an end.