- Just as was the case with Perl
yesterday, Java provides several methods to control flow.
Fortunately, control flow works exactly the same way in Java as
it works in Perl, so we don't have to learn too many new things
in this section.
- The following list should remind you
of the flow control structures available in Perl which are also
available in Java
- if / else if / else
if (age == 28)
{
System.out.println("You are 28!");
}
else if (age == 29)
{
System.out.println("You are 29!");
}
else
{
System.out.println("You're neither!");
}
- while
while (x <=100)
{
X++
System.out.println("x is: " x);
}
// In a while loop, the action is
// performed only if the test is true.
// An interesting alternative is
// the do / while loop that performs
// the action "before" testing.
- for
for (int x = 100;X >=0;x--)
{
System.out.println("x is: " x);
}
Testing a Condition
The Switch Statement
Breaks
- As you can see, Java also provides
a break statement that allows you to break out of a loop.
Previous Page |
Next Page
|