Previous Table of Contents Next


14.2.12. Statements

Many of Java’s control statements are similar or identical to C statements. This section lists and, where necessary, explains Java’s statements. Note that the topic of exceptions and the try/catch/finally statement is substantial enough that it is covered later in a section of its own.

14.2.12.1. The if/else, while, and do/while Statements

The if, else, do, and while statements are exactly the same in Java as they are in C. The only substantial difference arises because the Java boolean type cannot be cast to other types. In Java, the values 0 and null are not the same as false, and non-zero and non-null values are not the same as true.

The conditional expression that is expected by the if, the while, and the do/while statements must be of boolean type in Java. Specifying an integer type or a reference type won’t do. Thus, the following C code is not legal in Java:

   int i = 10;
   while(i—) {
       Object o = get_object();
       if (o) {
           do { ... } while(j);
       }
   }

In Java, you must make the condition you are testing for clear by explicitly testing your value against 0 or null. Use code like the following:

   int i = 10;
   while(i— > 0) {
       Object o = get_object();
       if (o != null) {
           do { ... } while(j != 0);
       }
   }

14.2.12.2. The switch Statement

The switch statement is the same in Java as it is in C. You may use byte, char, short, int, or long types as the values of the case labels, and you may also specify a default label just as you do in C.

14.2.12.3. The for Loop

The for statement is perhaps the most useful looping construct available in Java. There are only two differences between the Java for loop and the C for loop. The first difference is that although Java does not support the C comma operator (which allows multiple expressions to be joined into a single expression), the Java for loop simulates it by allowing multiple comma-separated expressions to appear in the initialization and increment sections, but not the test section, of the loop. For example:

   int i;
   String s;
   for(i=0, s = “testing”;             // Initialize variables.
       (i < 10) && (s.length() >= 1);  // Test for continuation.
       i++, s = s.substring(1))        // Increment variables.
   {
       System.out.println(s);          // Loop body.
   }

As you can see, this “difference” between the Java and C for loops is really a similarity.

The second difference is the addition of the C++ ability to declare local loop variables in the initialization section of the loop:

   for(int i = 0; i < my_array.length; i++)
       System.out.println(“a[“ + i + “] =    “ + my_array[i]);

Variables declared in this way have the for loop as their scope. In other words, they are only valid within the body of the for loop and within the initialization, test, and increment expressions of the loop. Although variables declared in for loops have their own scope, the Java compiler won’t let you declare a loop variable that has the same name as an already existing variable or parameter.

Note that because variable declaration syntax also uses the comma, the Java syntax allows you to either specify multiple comma-separated initialization expressions or to declare and initialize multiple comma-separated variables of the same type. You may not mix variable declarations with other, non-declaration expressions. For example, the following for loop declares and initializes two variables that are valid only within the for loop.

   for(int i=0, j=10; i < j; i++, j—) System.out.println(“k = “ + i*j);


Previous Table of Contents Next