-
There are many situations where we need to assign a variable to a different value depending on the result of a condition
-
For example, the
if-else-if
andswitch
statements in the previous section were used to decide which value to assign to the variablemonthName
-
A simpler example: Imagine your program needs to tell the user whether a number is even or odd. You need to initialize a
string
variable to either “Even” or “Odd” depending on whethermyInt % 2
is equal to 0. We could write anif
statement to do this:
Assignment with the conditional operator
-
If the only thing an
if
statement does is assign a value to a variable, there is a much shorter way to write it -
The conditional operator
?:
tests a condition, and then outputs one of two values based on the result -
Continuing the “even or odd” example, the conditional operator is used like this:
When this line of code is executed:
-
The condition
(myInt % 2 == 0)
is evaluated, and the result is either true or false -
If the condition is true, the conditional operator returns (outputs) the value
"Even"
(the left side of the:
) -
If the condition is false, the operator returns the value
"Odd"
(the right side of the:
) -
This value, either “Even” or “Odd”, is used in the initialization statement for
string output
-
Thus,
output
gets assigned the value"Even"
if(myInt % 2 == 0)
is true, or"Odd"
if(myInt % 2 == 0)
is false -
In general, the syntax for the conditional operator is:
-
The “condition” can be any expression that produces a
bool
when evaluated, just like in anif
statement -
true_expression
andfalse_expression
can be variables, values, or more complex expressions, but they must both produce the same type of data when evaluated -
For example, if
true_expression
ismyInt * 1.5
, thenfalse_expression
must also produce adouble
-
When the conditional operator is evaluated, it returns either the value of
true_expression
or the value offalse_expression
(depending on the condition) and this value can then be used in other operations such as assignment
Conditional operator examples
- The
true_expression
andfalse_expression
can both be mathematical expressions, and only one of them will get computed. For example:
If myInt
is even, the computer will evaluate myInt / 2
and assign
the result to answer
. If it is odd, the computer will evaluate
myInt + 1
and assign the result to answer
.
- Conditional operators can be used with user input to quickly provide a “default value” if the user’s input is invalid. For example, we can write a program that asks the user their height, but uses a default value of 0 if the user enters a negative height:
- The condition can be a Boolean variable by itself, just like in an if statement. This allows you to write code that looks kind of like English, due to the question mark in the conditional operator. For example,