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
statements. They are illustrated as follows:
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. The first form is called the Allman
style , named after programmer Eric Allman. We find the Allman style easier to read
and so we prefer it in this topic. The second form is called the Kernighan & Ritchie
or K&R style , named after Dennis Ritchie (the designer of C) and Brian Kernighan
(author of the first C tutorial). The K&R style saves lines, so some programmers
prefer it or some minor variant of it.
Be sure to note the indenting pattern in these examples.
 
Search WWH ::




Custom Search