Java Reference
In-Depth Information
3
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
different 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 boolean has only the two values, true and false , and Boolean expressions
evaluate to one of these two values. Before we discuss Boolean expressions and the
type boolean , we will introduce the Java branching statements using only Boolean
expressions whose meaning is intuitively obvious. This will serve to motivate our
discussion of Boolean expressions.
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
if-else Statements
An 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
compute a week's salary for an hourly employee. Assume the firm pays an overtime rate
 
Search WWH ::




Custom Search