In the previous lesson you learnt about the if...else... block.
The ternary operator
There is a short-cut technique you can sometimes use in place of simple if...else... blocks. Consider the following code:
The ternary operator allows you to combine the comparison with the assignment thus:
Enter your text here ...
The ternary operator consists of a number of parts:
- A variable to receive the result: i.e.,
String weather = - The condition to check: i.e.,
temperature > 25 - The question mark symbol:
? - The value to assign if the condition is true: i.e., the string "hot"
- A colon symbol
: - The value to assign if the condition is false: i.e., the string "not hot"
In the next lesson you will learn about conditional statements using switch... case...: