Selection:
`if'
Using pseudocode code we can illustrate how an if statement works. Consider the following pseudocode for a system to
operate fire alarms in an office building:
if
( temperature too high )
sound the alarm
This illustrates the general form of an if statement, i.e.:
if
<test>
action to perform if
test is true
To implement our fire alarm pseudocode as a
valid Java statement we could write the following:
if
(temperature > 78)
soundAlarm();
(of course we are assuming the class has a
variable called temperature and a method soundAlarm())
The way an
if statement can cause deviation from the
default sequential flow of execution can be illustrated as follows. Consider this
pseudocode design:
statementA;
if( age > 60 )
statementB;
statementC;
If the variable age has a value over 60, say 65,
then the test ( age > 60 ) will be true, and the therefore statementB will be executed. So the
statements to be executed will be:
statementA
statementB
statementC
However, if the variable age has a
value of 60 or less, say 21, then the test ( age > 60 ) will be false, and statementB will not be executed. So the statements executed will be:
statementA
statementC
Thus the result of a selection statement means
that some statements may not be executed, or may be executed once.
Back to top

RITSEC - Global Campus
Copyright ?1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg
|