java / beginner
Snippet
The Ternary Operator
The ternary operator is a concise way to write simple if-else statements in a single line. It evaluates a boolean expression and returns one of two values.
snippet.java
1
2
int score = 85;String result = (score >= 50) ? "Pass" : "Fail";
Breakdown
1
(score >= 50)
The condition to be evaluated (is score greater than or equal to 50?).
2
? "Pass"
The value returned if the condition is true.
3
: "Fail"
The value returned if the condition is false.