Java Reference
In-Depth Information
Multiple Statement Alternatives
In an if-else statement, you can have one or both alternatives contain several statements.
To accomplish this, group the statements using braces, as in the following example:
if (myScore > yourScore)
{
System.out.println("I win!");
wager = wager + 100;
}
else
{
System.out.println("I wish these were golf scores.");
wager = 0;
}
TIP: Placing of Braces
There are two commonly used ways of indenting and placing braces in if-else state-
ments. They are illustrated below:
if (myScore > yourScore)
{
System.out.println("I win!");
wager = wager + 100;
}
else
{
System.out.println("I wish these were golf scores.");
wager = 0;
}
and
if (myScore > yourScore) {
System.out.println("I win!");
wager = wager + 100;
} else {
System.out.println("I wish these were golf scores.");
wager = 0;
}
The only difference is the placement of braces. We find the first form easier to read and so
prefer the first form. The second form saves lines, so some programmers prefer the second
form or some minor variant of it.
Be sure to note the indenting pattern in these examples.
Search WWH ::




Custom Search