Booleans in Control Flow Statements

As you have already seen, Java is very strict when it comes to use of the boolean type. This strong typing also holds true in the restrictions placed on expressions that are allowed in the conditional clause of a control statement. These expressions are limited to variables (or literals) of the primitive type boolean and expressions that evaluate to a boolean result. Examples are shown in the following code fragments:

boolean testVal = false;
int intVal = 1;
...
if (testVal) { }  else { }
if (intVal != 1) { }  else { }
...
while (testVal) { }
while (intVal == 0) { }
...
do { }  while (testVal)



do { }  while (intVal == 0)
for (int j=0; testVal; j++) { }
for (int j=0; intVal < 5; j++) { }

Here, the conditionals ...

Get Special Edition Using Java 2 Standard 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.