Concept Flow - Unless for negated conditions
Evaluate condition
Is condition false?
No→Skip block
Yes
Execute block
Continue with rest of code
The 'unless' statement runs the code block only if the condition is false, skipping it otherwise.
x = 5 unless x > 10 puts "x is not greater than 10" end
| Step | Condition (x > 10) | Condition Result | Branch Taken | Output |
|---|---|---|---|---|
| 1 | 5 > 10 | false | Execute block | x is not greater than 10 |
| 2 | End of unless | - | Continue | - |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| x | 5 | 5 | 5 |
Syntax: unless condition # code runs if condition is false end Behavior: Runs block only when condition is false. Key rule: 'use unless' to check for negated conditions clearly.