Concept Flow - Ternary operator
Evaluate condition
Yes
Return value if true
End
No
Return value if false
End
The ternary operator checks a condition and returns one value if true, another if false, all in one line.
age = 20 status = age >= 18 ? "Adult" : "Minor" puts status
| Step | Condition (age >= 18) | Result | Value assigned to status | Output |
|---|---|---|---|---|
| 1 | 20 >= 18 | true | "Adult" | |
| 2 | N/A | N/A | N/A | Adult |
| Variable | Start | After Step 1 | After Step 2 (final) |
|---|---|---|---|
| age | undefined | 20 | 20 |
| status | undefined | "Adult" | "Adult" |
Syntax: condition ? value_if_true : value_if_false Evaluates condition once. Returns value_if_true if condition true. Returns value_if_false if condition false. Used for simple if-else in one line.