Java Reference
In-Depth Information
of two integer variables (named a and b ) to a third variable named min.
You could code as follows using a conventional if-else construct:
int a, b, min;
...
if (a < b)
min=a;
else
min=b;
With the conditional operator the code can be shortened and simpli-
fied, as follows:
min=(a<b)?a:b;
In the above statement the conditional expression is formed by the ele-
ments to the right of the assignment operator (=). There are three ele-
ments in the rvalue:
1. The expression ( a < b ) which evaluates either to logical true or false.
2. Theexpression?adeterminesthevalueassignedtothelvalueiftheexpres-
sion ( a < b ) is true.
3. The expression : b determines the value assigned to the lvalue if the expres-
sion ( a < b ) is false.
Programmers note:
The lvalue is the element to the left of the equal sign in an assignment
expression. The rvalue is the element to the right of the equal sign.
Search WWH ::




Custom Search