Science, Tech, Math Computer Science Ternary Operator Share Flipboard Email Print Computer Science Java Programming PHP Programming Language Perl Programming Language Python Programming JavaScript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More by Paul Leahy Updated August 10, 2017 The ternary operator "?:" earns its name because it's the only operator to take three operands. It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned: boolean expression ? value1 : value2 Examples:The following if..then..else statement: boolean isHappy = true; String mood = ""; if (isHappy == true) { mood = "I'm Happy!"; } else { mood = "I'm Sad!"; } can be reduced to one line using the ternary operator: boolean isHappy = true; String mood = (isHappy == true)?"I'm Happy!":"I'm Sad!"; Generally the code is easier to read when the if..then..else statement is written in full but sometimes the ternary operator can be a handy syntax shortcut. citecite this article Format mla apa chicago Your Citation Leahy, Paul. "Ternary Operator." ThoughtCo, Aug. 10, 2017, thoughtco.com/ternary-operator-2034302. Leahy, Paul. (2017, August 10). Ternary Operator. Retrieved from https://www.thoughtco.com/ternary-operator-2034302 Leahy, Paul. "Ternary Operator." ThoughtCo. https://www.thoughtco.com/ternary-operator-2034302 (accessed April 21, 2018). copy citation Continue Reading