Java Reference
In-Depth Information
3
Flow of Control
If you think we're wax-works,” he said, “you ought to pay, you know.
Wax-works weren't made to be looked at for nothing. Nohow!”
“Contrariwise,” added the one marked “DEE,” “if you think we're alive,
you ought to speak.”
LEWIS CARROLL,
Through the Looking-Glass
Introduction
As in most programming languages, Java handles flow of control with branching and
looping statements. Java branching and looping statements are the same as in the C
and C++ languages and are very similar to those in other programming languages.
(However, the Boolean expressions that control Java branches and loops are a bit dif-
ferent in Java from what they are in C and C++.)
Most branching and looping statements are controlled by Boolean expressions. A
Boolean expression is any expression that is either true or false. In Java, the primitive
type
, and Boolean expressions evalu-
ate to one of these two values. Before we discuss Boolean expressions and the type
has only the two values
and
boolean
true
false
, we will introduce the Java branching statements using only Boolean expres-
sions whose meaning is intuitively obvious. This will serve to motivate our discussion
of Boolean expressions.
boolean
Prerequisites
This chapter uses material from Chapters 1 and 2.
3.1
Branching Mechanism
When you come to a fork in the road, take it.
Attributed to Yogi Berra
Statements
if-else
statement chooses between two alternative statements based on the value
of a Boolean expression. For example, suppose you want to design a program to com-
pute a week's salary for an hourly employee. Assume the firm pays an overtime rate of
An
if-else
Search WWH ::




Custom Search