Chapter VI
Control Structures I
Chapter VI Topics
6.1
Introduction
6.2 Types
of Control Structures
6.3
Relational Operators
6.4
Command Line Prompt
6.5
One-Way Selection
6.6
Two-Way Selection
6.7
Multiple-Way Selection
6.8 Fixed Repetition
6.9
Conditional Repetition
6.10
Worked Out Exercises
6.11 Summary
6.1 Introduction
The previous chapter focused on using class methods and object methods, which is an integral part of Object Oriented Programming.
The intention in Exposure Java
is to present OOP in stages. A thorough
understanding of Object Oriented Programming is vital in modern computer
science. At the same time, you also need
to learn the syntax of a program language.
You cannot write proper Java OOP programs unless you know the Java
sentence structure or syntax. A
prerequisite for a creative writing course is knowledge of grammar.
Frequently, it is mentioned that program language
syntax is trivial. The essence of programming is design, data structures and algorithms. This is very true, and very lovely, but
language syntax tends to be trivial only if you know syntax. You will get very frustrated with your
programs when they do not compile because of syntax problems.
In an earlier chapter I mentioned that program
execution follows the exact sequence of program statements. That was true, but also a rather incomplete
explanation. There is a lot more to the
program flow picture. We can expand on
the exact program sequence by stating that program flow follows the sequence of
program statements, unless directed otherwise by a Java control structure.
|
Program Flow |
|
Program Flow follows the exact sequence of listed
program statements,
unless directed otherwise by a Java control structure. |
Programs in any computer language require control
structures. It may appear that all our
program examples were written without the use of any control structures, but
the control was subtle. As mentioned
earlier, control was provided by the sequence of program statements. That type of control is called simple sequence.
Simple sequence alone is not very satisfactory for
programs of any type of consequence.
Programs constantly make decisions.
A payroll program needs to change the amount paid, if the hours exceed
40 per week. The same payroll program
can have many variations of tax deductions based on the number of dependents
claimed. A payroll program also needs to
have the ability to repeat the same actions for additional employees. In other words, a program needs to have the
ability to repeat itself, or repeat certain program segments. The language features that allow that type of
control will be introduced in this chapter.
You will note that the title of this chapter is Control Structures I. You have guessed correctly. There will be a
second control structure chapter. The
second chapter will explain a variety of advanced control structure
features. At this stage, all we want to
do is get our feet wet and feel the temperature of the water. You will be diving in the deep-end before you
know it.
6.2 Types of Control Structures
Program-execution-flow is controlled by three
general types of control structures.
They are simple sequence, selection, and repetition. Java provides
syntax, and special keywords for each one of these three control
structures. Before we look at actual
Java source code required to implement control, let us first take a look at
diagrams that explain each control structure.
Simple Sequence

Simple sequence holds no surprises. A series of program statements are executed
in the exact sequence that they are written.
Altering the program sequence requires altering the sequence of the program
statements.
Selection
Frequently, programs cannot follow a single, simple
sequence, path. Decisions need to be
made. Should the applicant be hired or
not? Does the employee get overtime pay? Which tax bracket is the deduction to be
computed from?
Selection is also called conditional branching or decision making. The program flow encounters a special
condition. The value of the condition
determines if the program flow will “branch off” from the main program
sequence. Three diagrams will be
shown. The first diagram shows one-way selection, the second diagram
shows two-way selection, and the
third one shows multiple-way
selection.
One-Way Selection

Condition
True
![]()
![]()
False
Program Statement
![]()

Selection control structures use a special
conditional statement. If the condition
is true, some action is performed,
such as branching off to another sequence of program statements. In the case of one-way selection, the true condition
branches off. If the condition is false, the program flow continues
without change in program sequence.
Consider the analogy of driving South from Dallas to
Austin. Along the way you check if your
gas tank is low. If the tank is low, you
stop for gas, and then continue to Austin.
If the tank is not low you continue to drive south. Keep in mind that regardless of the tank
condition, you are heading to Austin.
Two-Way Selection

Condition![]()
![]()
True False


Program Statement
Program Statement
The two-way selection
control structure also checks to see if some special condition is true.
But there is a significant difference in how the program flow is
handled. With one-way selection, a true condition
means to do something, like get off the road and get gas, before
continuing. The two-way selection structure selects one direction, or the other
direction, but not both.
The one-way analogy describes a trip traveling South from Dallas to Austin. Regardless of the gas tank situation, the trip travels to Austin in the same car. Now consider an analogy for two-way selection. You are in Las Vegas gambling in the casino. You are placing all your money on one large bet. If you win the bet, you will fly home first-class, otherwise you will hitchhike home. Perhaps you argue that in both cases the final destination is home. True, but there are two totally different travel modes getting there.
Multiple-Way Selection

Condition
True
![]()
![]()
![]()
False
Condition Program Statement
True
![]()
False
Condition Program Statement
True
![]()
False
Condition Program Statement
True
![]()
False
![]()

Multiple selection is a commonly used control
structure that simulates many situations in real life. The program flow encounters a group of
conditions, one after the other. If the
condition is true, the program flow branches off to execute some other
statement. The main program flow is
rejoined below all the conditional statements.
Multiple selection in real life is encountered with
any multiple choice test, an ATM machine or any other situation where you
encounter more than two options that can be selected.
Repetition
Another common application occurs when repetition is required. A grade book program needs to average grades for every student in a class of twenty-five students. A payroll program needs to process paychecks for many employees. Practically everything you can imagine is done multiple times. Nobody is interested in repeating program source code 500 times for some task that is to be performed 500 times. We want to create one program segment, and place this segment in some type of loop control structure that repeats 500 times.

Condition
True
![]()
False


6.3 Relational
Operators
Both the selection control structure diagrams and
the repetition diagram indicate a change of program flow occurring after some condition. Understanding conditional statements is the
essence of understanding, and using, control structures. However, before we plunge into the syntax of
the various conditional statements, you need to understand the relational operators that are used by
Java in the conditional statements.
|
Conditional
Statement Definition |
|
A
conditional statement is a program expression that evaluates
to true or false. Most
conditional statements require a relational operator. All
conditions must be placed inside parentheses. |
Consider an expression, such as 5 + 4. Does that expression evaluate to true or false? Neither, it evaluates
to 9. A relational
operator is required to make an expression evaluate to true or false. Java has six relational operators: equals, not equals, greater than, less than,
greater than or equal, and less than or equal.
The idea of conditional statements that are based on
a relational operator can be considered in regular English statements:
If we save
more than $200.00 a month, we can go on a vacation
If your SAT
score is high enough you will be admitted to college, otherwise
you will not
be able to go to college
Repeat calling
established customers until you have 25 surveys
|
Name |
Operator |
Expression |
Evaluates |
|
Equals |
== |
5 == 5 K == 10 |
true depends |
|
Not equals |
!= |
50 != 25 100 != 100 |
true false |
|
|