Java uses the same conditional expression (sometimes called a "ternary operator") as C and C++.
This performs a
conditional test in an expression, resulting in the first value if
the condition is true,or the second if the condition is false
Note: due to operator
precedence issues, it is often best to enclose the entire expression
in parentheses. Here's a brief example:
Note that the parentheses around the test are not
necessary by the operator precedence rules, but may help make the
The parentheses around the entire conditional expression
are necessary; without them, precedence rules would concatenate the
boolean result onto the initial string, and then the
would be flagged as an error, since the value to its left would not be a
do . . . while Loops
Again, the loops in
Java are pretty much the same as in C - with the exception that
the conditional expressions must evaluate to only
for loop uses a counter to progress through a series of
- A value is initialized, tested each time
through, and then modified (usually incremented) at the end of each
pass, before it is tested again.
for loop does not do anything that cannot be done with
while loop, but it puts everything that controls the
looping at the top of the block.
for loops can use a variable declared outside of the control portion of the loop or in the control portion. The latter gives the variable block-level scope
(existing only for the duration of the loop).
for (j = 0; j < 12; j++ )
System.out.print("j = " + j);
for (int j = 0; j < 12; j++ )
System.out.print("j = " + j);
for loop may use multiple control variables by using
the sequence operator, the comma (
for (int j = 0, k = 12; j <= 12; j++, k-- )
System.out.println(j + " " + k);
Note: if you use a block-scope variable (such as above)
for the first counter, the additional variables will be block scope as
well, and also will be the same type of data - i.e., the variable
also exists only for the duration of the block. There is no way to declare
two counters of different types at block-level scope.
Note that neither
do . . . while loops allow declaring
a looping variable with this type of scope. The looping variable must be
declared in advance.
The for-each loop allows us to loop through an array or collection class instance using a simplified
The looping variable is not a counter - it will contain each element of the
array or collection in turn (the actual value and not an index to it, so its
type should be the same as the type of items in the array or collection).
You can read the
as if it is the word "from". We will cover this type of loop in
more depth in the Arrays section.
The looping variable must be declared within the parentheses
controlling the loop - you cannot use a preexisting variable.
Since the looping variable is a local variable, it gets a copy of each value
from the array or collection. Therefore you cannot use the for-each loop to
write values back to the array/collection - assigning a new value to the
variable in the body of the loop is only overwriting the local copy.
In the next exercise, we will ask you to improve the guessing game using a loop - the user will be able to continue guessing until the correct answer is given.