Java Reference
In-Depth Information
It is a personal choice, which of the above piece of code to use. Finally, note that if you type two or more
semicolons where only one was required, it would not cause any errors, because each extra semicolon is considered
as an empty statement. For example,
i++; // Ok. Here, semicolon is part of statement
i++;; // Still Ok. The second semicolon is considered as empty statement.
You cannot use an empty statement where a statement is not allowed. For example, when only one statement is
allowed, adding an extra empty statement will cause an error, as shown in the following snippet of code. It associates
two statements, i++; and an empty statement ( ; ), to an if statement, where only one statement is allowed.
if (i == 10)
i++;; // A compile-time error. Cannot use two statements before an else statement
else
i--;
Summary
A statement in a Java program specifies an action. Statements in Java can be broadly classified in three categories:
declaration statements, expression statements, and control flow statements. A declaration statement is used to declare
variables. An expression statement is used to evaluate an expression. A control flow statement controls the order in
which other statements are executed. Control flow statements include if , if-else and looping statements. A looping
statement executes a block of statements repeatedly until some condition becomes false. Java provides four looping
statements: for loop, for-each loop, while loop, and do-while loop. A break statement is used to transfer control
outside of a block statement or a loop. A continue statement is used to ignore executing the remaining code for a loop
and continue with the next iteration. Java has an empty statement, too, which is simply a semicolon by itself.
 
Search WWH ::




Custom Search