5.3 Selection Using if/else

The second form of an if statement is appropriate when the data falls into two mutually exclusive categories and different instructions should be executed for each category. For these situations, we use an if/else statement, which has the following pattern:

if ( condition )
{
   true block
}
else
{
   false block
}
next statement

If the condition evaluates to true, the true block is executed and the false block is skipped. If the condition evaluates to false, the true block is skipped and the false block is executed. In either situation, the statement following the if statement is executed next. Figure 5.5 illustrates the flow of control of an if/else statement.

If the true or false block contains only one statement, ...

Get Java Illuminated, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.