Concept Flow - Inline if and unless (modifier form)
Start
Evaluate condition
Execute statement
Skip statement
End
The program checks a condition and executes a statement only if the condition is true (for if) or false (for unless), all in one line.
x = 10 puts "x is 10" if x == 10 puts "x is not 5" unless x == 5
| Step | Code Line | Condition | Condition Result | Action | Output |
|---|---|---|---|---|---|
| 1 | x = 10 | - | - | Assign 10 to x | - |
| 2 | puts "x is 10" if x == 10 | x == 10 | true | Print 'x is 10' | x is 10 |
| 3 | puts "x is not 5" unless x == 5 | x == 5 | false | Print 'x is not 5' | x is not 5 |
| Variable | Start | After 1 | After 2 | Final |
|---|---|---|---|---|
| x | nil | 10 | 10 | 10 |
Inline if/unless modifier form: statement if condition statement unless condition Executes statement only if condition is true (if) or false (unless). Good for short conditional actions on one line.