Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

Program Control Methods

• Three basic methods of control are used in designing algorithms


and programs:
1. Sequential: Steps are performed in a strictly sequential
manner, each step being executed exactly once.
2. Selection: One of a number of alternative actions is selected
and executed.
3. Repetition: One or more steps are performed repeatedly.
• These three control structures can be combined to create just
about any desired algorithm.
• In the last lecture, sequential control was covered.
• In this lecture, we will cover selective control.

1
Logical Expressions
• Logical expression is an expression which evaluates
to a logical value
• Only two possible values exist for logical data types:
true and false
• These values are represented in Fortran by: .TRUE.
AND .FALSE.

2
Simple Logical Expressions
• Most simple logical expressions will be of the type:
expression1 relational-operator expression2

• where expression1 and expression2 are numeric,


character or logical expressions
• and relational-operator may be any of the following:

Symbol Meaning
< or .LT. Is less than
> or .GT. Is greater than
== or .EQ. Is equal to
<= or .LE. Is less than or equal to
>= or .GE. Is greater than or equal
to
3
/= or .NE. Is not equal to
Compound Logical Expressions
• Compound logical expressions are formed by combining logical
expressions using the logical operators:

.NOT. (negation)
.AND. (conjunction)
.OR. (disjunction)
.EQV. (equivalence)
.NEQV. (nonequivalence)

• If a logical expression contains arithmetic operators, relational


operators and logical operators, the operations are performed in the
following order:
1. Arithmetic operations (and functions)
2. Relational operations
3. Logical operations in the order listed above

4
Compound Logical Expressions
• Examples: assume that n = 4 :

n > 1 .AND. n > 2


n**2 + 1 > 10 .AND. n < 3
n == 3 .OR. n == 4
n == 4 .AND. .NOT. n < 5

5
Simple IF Construct
In the simplest selection structure, a sequence (or
block) of statement is executed or bypassed depending
on whether a given logical expression is true or false.
This can be diagramed as follows:

true logical
expression

statement false
sequence

continue

6
IF-THEN Syntax

Required parentheses

IF ( logical_expression ) THEN One line or continued

block of statements
Block is executed
when logical_expression
END IF is .TRUE. but skipped
otherwise

END IF can be one or two words

7
Simple IF Construct
This selection structure is implemented in Fortran by using an
IF construct of the form:
IF (logical-expression) THEN
statement-sequence
END IF

If the logical expression is true, the specified sequence of


statements is executed; otherwise it is bypassed. In either
case, execution continues with the statement in the program
following the END IF.

Example: These statements


IF (x >= 0.) THEN only executed when x
y = x * x is greater than or
z = SQRT(x) equal to zero
END IF
8
Simple IF Construct
If the statement sequence consists of a single statement, there is
a short version, called an IF statement:
IF (logical-expression) statement

If the logical expression is true, the specified statement is


executed; otherwise it is bypassed. In either case, execution
continues with the next statement in the program.
Example:
IF (x >= 0.0) y = SQRT(x)

9
General IF Construct
In the preceding selection structure, the selection is made
between
(1) executing a given sequence of statements and
(2) bypassing these statements.
In the two-way selection pictured in the following diagram, the
selection is made between
(1) executing one sequence (block) of statements and
(2) executing a different block of statements:

true logical false


expression

statement statement
sequence1 sequence2

continue

10
IF-THEN-ELSE Syntax

IF ( logical_expression ) THEN

block_1
Block_1 is executed
when logical_expression
is .TRUE.

ELSE A line by itself

Block_2 is executed
block_2
when logical_expression
is .FALSE.
END IF
11
General IF Construct (cont.)
This selection structure is implemented in Fortran by an IF construct of the
form:

IF (logical-expression) THEN Executed when true


statement-sequence1
ELSE
statement-sequence2
END IF
Executed when false

Example:
IF (x > 0.) THEN
y = SQRT(x)
ELSE
x = -x
y = SQRT(x)
END IF
12
IF-ELSE IF Constructs
Often, selection structures need more than two branches, that
is, more than two “statement-sequences”. In this case, IF-ELSE
IF constructs are used:

IF (logical-expression1) THEN
statement-sequence1
ELSE IF (logical-expression2) THEN
statement-sequence2
ELSE IF (logical-expression3) THEN
.
.
.
ELSE
statement-sequencen
END IF

13
Example
• Suppose we need a program segment to read a number x and display
its sign. More precisely, if x is positive, a + is displayed; if x is
negative, a - is displayed; otherwise, a 0 is displayed.

IF (x > 0) THEN
WRITE(*,*) '+'
ELSE IF (x == 0) THEN
WRITE(*,*) '0'
ELSE
WRITE(*,*) '-'
END IF

14
Example
• Write a code that type a letter grade based on the score that is entered
by the user, knowing that
– Score less than 50, 'F'
– Score less than 60 and greater or equal 50 'D'
– Score less than 75 and greater or equal 60 ‘C'
– Score less than 85 and greater or equal 75 ‘B'
– Score greater than 85 ‘A'
Real :: x
CHARACTER(LEN=1) :: Grade
IF (x < 50) THEN
Grade = 'F'
ELSE IF (x < 60) THEN
Grade = 'D'
ELSE IF (x < 75) THEN
Grade = 'C'
ELSE IF (x < 85) THEN
Grade = 'B'
ELSE
Grade = 'A'
15
END IF
Example
• find the smallest of three given numbers from screen

Read(*,*) a,b,c
IF (a < b .AND. a < c) THEN
Result = a
ELSE IF (b < a .AND. b < c) THEN
Result = b
ELSE
Result = c
END IF

16
Application: Air Pollution Index
The level of air pollution in the city of Foggy Bottom is
measured by air pollution index readings taken daily
(at noon) at three locations: the Brown Palace, Rarified
Avenue and AYCS Mall. The integer average of these
three readings is the pollution index, and a value of 50
ppm or greater represents a hazardous condition. This
index is calculated daily, and the Foggy Bottom
Environmental Engineer would like a program that
calculates the pollution index and then determines the
appropriate condition, safe or hazardous.

17
Example: Modified Pollution-
Index Problem
As an example of an IF-ELSE IF construct,
suppose that in the pollution index problem,
three air-quality conditions – good, fair and
poor – are to be used instead of the two that
we used. Two cutoff values will be used:
LowCutoff and HighCutoff, both in parts per
million. A pollution index less than LowCutoff
indicates a good condition; an index between
LowCutoff and HighCutoff , a fair condition;
and an index greater than HighCutoff a poor
condition. 18

You might also like