November 2013
Beginner
325 pages
9h 47m
English
It is not uncommon to use if and else to set the value of an instance variable. For example, you might have the following code:
int minutesPerPound;
if (isBoneless) {
minutesPerPound = 15;
} else {
minutesPerPound = 20;
}
Whenever you have a scenario where a value is assigned to a variable based on a conditional, you have a candidate for the conditional operator, which is ?. (You will sometimes see it called the ternary operator because it takes three operands).
int minutesPerPound = isBoneless ? 15 : 20;
This one line is equivalent to the previous example. Instead of writing if and else, you write an assignment. The part before the ? is the conditional. The values after the
Read now
Unlock full access