Concept Flow - Ternary conditional operator
Evaluate condition
Yes/No
True
Return true_value
End
The ternary operator checks a condition and returns one value if true, another if false.
int a = 10; int b = 20; int max = (a > b) ? a : b; Console.WriteLine(max);
| Step | Condition (a > b) | Result of Condition | Value if True | Value if False | Variable max | Output |
|---|---|---|---|---|---|---|
| 1 | 10 > 20 | False | 10 | 20 | 20 | 20 |
| 2 | End | - | - | - | 20 | Printed 20 |
| Variable | Start | After Step 1 | Final |
|---|---|---|---|
| a | 10 | 10 | 10 |
| b | 20 | 20 | 20 |
| max | undefined | 20 | 20 |
Syntax: condition ? value_if_true : value_if_false; Evaluates condition once. Returns value_if_true if condition is true. Returns value_if_false if condition is false. Used for simple if-else assignments. Example: int max = (a > b) ? a : b;