Blocks

The if statement normally expects only one statement in its body. To include several statements in the body of an if (or the body of an else for an if...else statement), enclose the statements in braces. Statements contained in a pair of braces (such as the body of a method) form a block. A block can be placed anywhere in a method that a single statement can be placed.

The following example includes a block in the else part of an if...else statement:

if (grade >= 60)   System.out.println("Passed");else{   System.out.println("Failed");   System.out.println("You must take this course again.");}

In this case, if grade is less than 60, the program executes both statements in the body of the else and prints ...

Get Java™ How To Program (Early Objects), Tenth 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.