Java Reference
In-Depth Information
Controlling the Flow of
Your Program
What you Will learn in this chapter:
➤
How to determine if a certain condition is met
➤
How to control what a Java program does and when it should do it
➤
How to use loops in Java to repeat an action
➤
How to determine which control structure to use
Wrox.com code doWnloads for this chapter
beginningjavaprogramming
on the Download Code tab. The code is in the Chapter 5
download and individually named according to the names throughout the chapter.
Control structures allow a programmer to define how and when certain statements will be
executed. In other words, if certain conditions are met, specified behaviors will result. Several
structures exist in Java and other languages, such as loops and if-then statements, which allow
this control to be implemented. This chapter begins by introducing (or re-introducing) some
operators that will be used extensively in control structures. The operators, essentially, allow you
to compare values. In order to make similar comparisons on different data types, some compara-
tive methods will also be introduced. Once you can make these comparisons and assessments,
you can start using them to make decisions. That is where the if-then statement comes in. In its
simplest form, this structure can be read as follows: if some condition is true, then do something.
Next, you will look at
for
and
while
loops, and some extensions of these, which allow sections
of code to be repeated based on the conditions you define. A
switch
is an alternative structure
that's similar to an if-then statement, but offers a list of cases that can be defined so each is
Search WWH ::
Custom Search