Java Reference
In-Depth Information
usually used in plain assignments, they also make perfect sense as illustrated
by the program below:
Program 2.8
Boolean arithmetic expression
class
Boolean
{
public static void
main( String [ ]
args )
boolean
b1 = (6
−
2) == 4;
boolean
b2 = 22/7 == 3+1/7.0 ;
boolean
b3 = 22/7 == 3+ 1/7;
System . out . println (b1) ;
// true
System . out . println (b2) ;
// false
System . out . println (b3) ;
// true
}
}
2.5 Unfolding loops and program termination
2.5.1 Unfolding loops
When executing a program that contains loop structures, we can unroll these
loops manually. Compilers actually do it sometimes to optimize the generated
bytecode.
2.5.2 Never ending programs
Once programmers first experience loops, a major issue arises: Does the
program terminate? It is indeed quite easy to write never ending programs
by writing loops that execute forever as illustrated below:
int i=0;
while (true)
i++;
Always make sure when you write a
for
structure that the boolean expression
will evaluate to
false
at some stage. Take care to avoid mistyping problems
such as:
for(i=0;i>=0;i++)
; // common mistyping error in the boolean predicate
... and prefer to use curly brackets instead of the semi-colon for single-
instruction blocks:
Search WWH ::
Custom Search