Looping
Java
has three standard Looping constructs: while,
do-while,
and for. You'll get the full loop scoop later
in the
book, but not for awhile, so let's do while for
now.
The
syntax (not to mention logic) is so simple
you're
probably asleep already. As long as some
condition
is true, you do everything inside the
loop
block. The loop block is bounded by a pair of
curly
braces, so whatever you want to repeat needs
to be
inside that block.
The key
to a loop is the conditional test. In Java, a
conditional
test is an expression that results in a
boolean
value-in other words, something that is
either
true or false.
You can
do a simple boolean test by checking
the
value of a variable, using a comparison operator
including:
<
(less than)
>
(greater than)
==
(equality) (yes, that's two equals signs)
Notice
the difference between the assignment
operator
(a single equals sign) and the equals
operator
(two equals signs).
Int x =
4 ;
while (
x > 3)
{
//loop
code will run because
//x is
greater than 3
x = x
-1 ; // or we’d loop forever
}
0 Comments