Java Reference
In-Depth Information
The If…Then…Else structure represents a two-way decision made in the logic of the program.
The decision is made on the basis of a condition that must be satisfied. If the condition is not satisfied,
the program logic executes one action. If the condition is satisfied, the program logic executes a differ-
ent action. This type of logic structure is shown in Figure A-8a. The If…Then…Else structure also can
result in a decision to take no action, as shown in Figure A-8b. The flowcharts presented in
Figures A-4 and A-5 on pages A.05 and A.06 include this logic structure.
False
True
True
Condition
Condition
Action 1
False
Action 1
Action 2
(b)
(a)
If...Then...Else Structure
FIGURE A-8
False
The Do While structure is the logic structure most commonly used
to create a process that will repeat as long as the condition is true. The Do
While structure is illustrated in Figure A-9 and has been used earlier in
Figures A-2, A-5, and A-6. In a Do While structure, the decision to perform
the action within the structure is at the top of the loop; as a result, the
action will not occur if the condition is never satisfied.
The Do Until structure (Figure A-10) also is used for creating a process
that will be repeated. The major differences between the Do Until and the
Do While structures are that (1) the action within the structure of a Do
Until always will be executed at least once, (2) the decision to perform
the action within the structure is at the bottom of the Do Until loop, and
(3) the Do Until loop exits when the condition is true.
Figure A-10 illustrates the Do Until structure, and the flowchart
presented in Figure A-4 on page A.05 includes a Do Until structure.
The Select Case structure is similar to the If…Then…Else structure
except that it provides more than two alternatives. Figure A-11 illustrates the
Select Case structure.
A logical solution to a programming problem can be developed through
the use of just these five logic structures. The program will be easy to read,
easy to modify, and reliable; most important of all, the program will do
what it is intended to do.
Condition
True
Action(s)
Do While Structure
FIGURE A-9
Action(s)
True
Condition
False
Do Until Structure
FIGURE A-10
 
Search WWH ::




Custom Search