There are many situations where the syntax of Java requires a single statement. A sequence of statements can be considered to be a single statement if they are turned into a block (which is called a compound statement in some other languages). This is done by surrounding the sequence of statements by { and }. Examples of this appear in the following section.
Java has 2 kinds of conditional statements. Here are some examples of an if statement:
0066: if (a < 0) if (a > b) if (a > b) {
0067: a = -a; larger = a; larger = a;
0068: else System.out.println("a>b");
0069: larger = b; }
0070: else {
0071: larger = b;
0072: System.out.println("a<=b");
0073: }
Although C (and C++) allow the condition after the if to have an arithmetic value, this is not permitted in Java: the condition must be a boolean expression.
Here are two examples of a switch statement:
0074: switch ( dayNumber ) { switch ( dayNumber ) {
0075: case 2: case 3: case 4: case 1:
0076: case 5: case 6: readRatherHeavyNewspaper();
0077: gotoWork(); break;
0078: doWork(); case 7:
0079: goHome(); break;
0080: watchTV(); default:
0081: gotoPub(); gotoWork();
0082: break; doWork();
0083: case 7: goHome();
0084: break; watchTV();
0085: case 1: gotoPub();
0086: readRatherHeavyNewspaper(); }
0087: }
Following the symbol switch, there should be an expression which is enclosed by parentheses. In the above examples, this expression is on the line:
0074: switch ( dayNumber ) { switch ( dayNumber ) {
and it just consists of the variable
dayNumber.
The expression
should be of type
char,
byte,
short,
or
int.
When the switch statement is executed, the expression is evaluated and then control is passed to the statement whose associated case label has a value equal to that of the expression. If there is no such statement, then control is passed to the statement associated with the default label if there is one; otherwise, control is passed to the statement following the switch statement.
A break statement must be executed if you wish to leave the switch statement before the last statement of the switch statement. So, normally, there will be a break statement just before each case label and before the default label (if there is one).
Java has 3 kinds of looping statements. Here is an example of a for statement:
0088: int numMonths = Integer.parseInt(input.readLine());
0089: int rainfallSum = 0;
0090: for ( int monthNum = 0; monthNum < numMonths; monthNum++ ) {
0091: int figureForMonth = Integer.parseInt(input.readLine());
0092: rainfallSum += figureForMonth;
0093: }
0094: System.out.println(rainfallSum);
The above example has the variable monthNum declared in the for statement itself. If you do this, then this variable can only be used within the for statement. If you leave out the type, then the variable must be declared elsewhere and the variable is similar to any other variable of the block containing the for statement.
Here is an example of a while statement:
0095: int rainfallSum = 0;
0096: int figureForMonth = Integer.parseInt(input.readLine());
0097: while ( figureForMonth >= 0 ) {
0098: rainfallSum += figureForMonth;
0099: figureForMonth = Integer.parseInt(input.readLine());
0100: }
0101: System.out.println(rainfallSum);
Here is an example of a do statement:
0102: int rainfallSum = 0;
0103: do {
0104: int figureForMonth = Integer.parseInt(input.readLine());
0105: if ( figureForMonth >= 0 )
0106: rainfallSum += figureForMonth;
0107: } while ( figureForMonth >= 0 ) ;
0108: System.out.println(rainfallSum);
Although C (and C++) allow the condition of a while statement or a do statement to have an arithmetic value, this is not permitted in Java: the condition must be a boolean expression.
A break statement terminates the execution of a for, do, while or switch statement, and transfers control to the statement following that statement. A break statement may include a label, and this label indicates that it is the statement with that label that is to be terminated.
A continue statement transfers control to end of the current iteration of a for, do or while statement. A continue statement may include a label, and, if this is the case, control skips to the end of the loop that has this label.
Java also has try statements, catch clauses, finally clauses, and throw statements. These are all used for exception handling, and details about these will be given later.