Ternary Operators! | Operators | Operands
Operators: the “symbols that instruct the computer to perform a single, simple task, and most consist of just one or two characters”. (Icarus)
Operands: the symbols appearing on either side of the operators
Here are the Three types of Operators
Operators & Operands
- Unary operator → 1 Operand
- Binary Operator → 2 Operands
- Ternary Operator → 3 Operand
How do Ternary Operators work?
1 ? 2 : 3
Example:
5 > 4 ? Yessir : No Sir
This will return “Yessir” as 5 > 4 is a True Condition, returning a true action.
6 < 4 ? Yessir : No Sir
This will return “No Sir” as 6<4 is a False Condition, returning a False action.
Practice Problem
I was given a practice problem to find the maximum using a function, and here are the three ways of doing it. (Ternary Operator solution at the end)
Simple Solutions
Ternary Operator Solution!
“Code that works works, it doesn’t have to be complex code. However, a complex line of code like this makes you look genius material” — favorite code from lecturer :)