Statements in Java
Statements in Java are very similar to expressions in C or C++.
- The empty statement is a placeholder and does nothing.
- A variable declaration is a statement, whether it contains
an initialization or not.
- A labeled statement is a statement that begins with a label
(a name followed by :)
- Most statements are expressions statements, like assignments,
increments, decrements and method calls.
- A selection statement causes a choice to be made depending
on a value. There are if, if-else and switch-case-default
statements.
- Iterations statements specify how looping will take place.
for, while and do while require the control expressions
to have a boolean type. A construct like
while (i) --i;
is legal in C but not in Java. In Java you must write
while (i!=0) --i;
- A jump statement passes control to the beginning or end
of the current block or a labeled statement.
The break and continue statements have an optional
label argument that indicates where to jump to.
Java has no goto statement.
return is also a jump statement.
throw jumps to code for handling an exception.
- A synchronization statement is used to ensure that some
variables are locked against simultaneous use in multi-threaded applications.
- A guarding statement, like try, catch and
finally is used to handle exceptions.