Page 482 - Introduction to Programming with Java: A Problem Solving Approach
P. 482
448 Chapter 11 Type Details and Alternate Coding Mechanisms
assignment inside the while condition. If, for example, the user responds to the prompt by entering 80, score gets the value 80, the assignment expression within the parentheses evaluates to 80, and the while loop header becomes:
while (80 != -1)
Since the condition is true, the JVM executes the body of the loop. If the assignment expression were not embedded in the while loop condition, it would have to appear twice—once above the loop header and again at the bottom of the loop. Embedding the assignment in the condition improves the loop’s structure.
You will sometimes also see embedded assignments in method arguments and array indices. This makes code more compact. Compactness is often a good thing in that it can lead to code that is less clut- tered and therefore easier to understand. But don’t go too far in trying to make your code compact because compactness can sometimes lead to code that is harder to understand (i.e., it can lead to code that is more cryptic). Some programmers get a kick out of making “clever” programs that are as compact as possible. If that’s you, try to redirect your efforts to making programs as understandable as possible. You can still use compact code, but do so in a manner that helps, not hinders, understandability.
11.7 Conditional Operator Expressions
This section supplements the material in Chapter 4, Section 4.3 (if Statements).
Syntax and Semantics
When you want a logical condition to determine which of two alternate values applies, instead of using the
Apago PDF Enhancer
“if, else” form of the if statement, you can use a conditional operator expression. The conditional operator is Java’s only ternary operator. Ternary means three. The conditional relates three operands with the two symbols, ? and :. The ? goes between the first and second operands, and the : goes between the second and third operands.
Here’s the syntax:
<condition> ? <expression1> : <expression2>
If the condition is true, the conditional operator expression evaluates to the value of expression1, and it ignores expression2. If the condition is false, the conditional operator expression evaluates to the value of expression2, and it ignores expression1. Think of expression1 as the true part of an “if, else” statement. Think of expression2 as the false part of an “if, else” statement.
For example, consider this expression:
(x>y) ? x : y
The parentheses around the condition are not required, because > has higher precedence than the ?: pair, but we recommend using them because they improve readability. What does the JVM do when it sees this expression?
• It compares x with y.
• If x is greater, it evaluates the expression to x.
• If x is not greater, it evaluates the expression to y.
Do you know what general functionality the expression implements? It finds the maximum between two numbers. You can prove this to yourself by plugging in sample numbers. Suppose x 2 and y 5. Here’s how the expression evaluates to the maximum, 5: