Java Reference
In-Depth Information
Chapter 2 defined a program as a sequence of statements whose objective is to accomplish
some task. The programs you have examined so far have been simple and straightforward.
In executing programs, the computer starts at the first (executable) statement and executes
the statements in order until it comes to the end. In this chapter and in Chapter 5, you
will learn how to tell a computer that it does not have to follow a simple sequential order
of statements; it can also make decisions and/or repeat certain statements over and over
until certain conditions are met.
Control Structures
A computer can process a program in one of three ways:
￿ In sequence
￿ By making a selection or a choice, which is also called a branch
￿ By repetition, executing a statement over and over using a structure
called a loop
These three types of program flow are shown in Figure 4-1. The programming examples
in Chapters 2 and 3 show simple sequential programs. With such a program, the
computer starts at the beginning and follows the statements in order. No decisions are
made and there is no repetition.
statement1
logical
expression
logical
expression true
false
true
statement
statement2
.
.
.
statement2
statement1
false
statementN
a. Sequence
b. Selection
c. Repetition
FIGURE 4-1 Flow of execution
Control structures provide alternatives to sequential program execution and are used to
alter the flow of execution. The two most common control structures are selection and
 
Search WWH ::




Custom Search