You are on page 1of 37

Basics of Computer

Sciences
By
Dr. Ali Kamel Abdel-Rahman
Mechanical Engineering Department
Decision Structures in FORTRAN

• In addition to the elementary arithmetic


operations, the ALU in the computer is designed
to compare two items and execute instructions
based on that comparison.
• In the most elementary type of comparison, the
two items are either the same or not the same.
• A new form of expression has been added to
FORTRAN. It is called the logical expression,
and it constitutes the central ingredient in
decision structures.

Dr. Ali K. Abdel-Rahman


Logical Block IF Structures

• The FORTRAN structure that is the keystone of


all decision procedures is the logical block IF
structure, which take the following form:

IF ( <logical expression> ) THEN


This set or block of FORTRAN statements
will be executed only if the logical
expression is evaluated as true.
ENDIF

Dr. Ali K. Abdel-Rahman


Logical Expressions

• The action of the IF statement depends on the


definition of the logical expression.
• A logical expression is built up from combinations
of one or more relational expressions of the form.

a1 op a2
where a1 and a2 are arithmetic expressions, variables,
constants, or character strings; in short, things that have
values that can be compared.

Dr. Ali K. Abdel-Rahman


Logical Expressions

• The “op” is a relational logic operator belonging


to the following set:

Relational
Logic Operator Meaning
.EQ. Equal
.NE. Not equal
.LT. Less than
.LE. Less than or equal
.GT. Greater than
.GE. Greater than or equa1

Dr. Ali K. Abdel-Rahman


Logical Expressions

• Logical variables may only have a value of


<true> or <false>).
• The simplest logical expression consists of a
single relational expression like:
12 .GT. 6 This expression has a value of <true>

• A logical expression may then be incorporated


in an IF block structure, as in
IF(TEMP .GT. 450.0) THEN
PRINT*, „Steam temperature is dangerously high‟
STOP
ENDIF
Dr. Ali K. Abdel-Rahman
Logical Expressions

• More complex logical expressions can be built up


by combining two or more relational expressions
by means of the combinational operator.

Combinational
Logic Operator Meaning
.OR. Or
.AND. And
.NOT. Not (Changes a value <true> into
a value <false> and vice versa)

Dr. Ali K. Abdel-Rahman


Logical Expressions

• The evaluation of logical expressions is fairly


transparent, as can be seen from the following
examples.
1- If the variable SIZE has previously been
assigned the value 12.0, then the expression
SIZE .LT. 100.0 has a value of <true>.
2- All arithmetic operations are processed
before the logical expression is evaluated.
SIZE .LT. 10.0*SQRT(100.0)
has the same value as the previous expression.

Dr. Ali K. Abdel-Rahman


Logical Expressions

3- Parentheses may be added for clarity or to


alter the value of the expression.
(SIZE-6.0) .LT. (10.0*SQRT(100.0)-50.0)
4- Logical sub-expression may be combined by
using the operators .AND. and .OR. and are
then evaluated according to the following
rules:
<true> .AND. <true> = <true>
<true> .AND. <false> = <false>
That is, the entire expression is <false> if either side of
the .AND. is <false>.
Dr. Ali K. Abdel-Rahman
Logical Expressions

All possible cases are shown in the following


example:
A .AND. B
If A<true> and B<true>, logical value of expression is <true>
If A<false> and B<true>, logical value of expression is <false>
If A<true> and B<false>, logical value of expression is <false>
If A<false> and B<false>,logical value of expression is <false>

Dr. Ali K. Abdel-Rahman


Logical Expressions

<true> .OR. <false> = <true>


<false> .OR. <false> = <false>
The entire expression is <true> if either side of the .OR.
is <true>.
For example, if the variables A and B have values 2.0
and 8.0, respectively, then
A .GT. 6.0 .AND. 2.0*B .LT. 20.0
is evaluated as <false> .AND. <true>, which is then
<false>
A*B .EQ. 0.0 .OR. A .LT. 0.0
is evaluated as <false> .OR. <false>, which is then
<false>

Dr. Ali K. Abdel-Rahman


Logical Expressions

All possible cases are shown in the following


example:
A .OR. B
If A is <true> and B is <true> then the logical value is <true>
If A is <true> and B is <false> then the logical value is <true>
If A is <false> and B is <true> then the logical value is <true>
If A is <false> and B is <false> then the logical value is <false>

Dr. Ali K. Abdel-Rahman


Evaluation Order of Operations in a Logical Expression

• A logical expressions are evaluated in this order:


1- All arithmetic expressions
2- Relational logic operators
(.EQ.,.NE.,.GE.,.LE.,.GT.,.LT.) are processed
scanning left to right.
3- Combinational logic operators (the .NOT.,
.AND., and .OR. operators processed) from
left to right, with the .AND. s processed
before the .OR. s.

Dr. Ali K. Abdel-Rahman


Potential Pitfalls

• When using logical IF test, two “rules” will help


you avoid some commonly made errors:
1- Never test for the equality of real numbers obtained
from computation. For example, if
A = 2.0
B = SQRT(2.0)
B = B**2
IF(A .EQ. B) THEN
STOP
ENDIF
The test may possibly fail since B could have been
assigned the value 1.9999999.

Dr. Ali K. Abdel-Rahman


Potential Pitfalls

2- If it is necessary to test whether a quantity is smaller


than some very small number, say EPS, the form of the
test should never be
IF(X .LT. EPS) THEN
but rather
IF(ABS(X) .LT. EPS) THEN
The point is that X might be negative, and any negative
number, regardless of size, would satisfy the first IF
test.
The test for the equality of the two real, A and B,
should then possibly read
IF(ABS(A-B) .LT. EPS) THEN
Dr. Ali K. Abdel-Rahman
IF(…)THEN-ELSE Structure

• Frequently, an algorithm will have two


computational branches as a result of a logical
IF test.
• If the condition is <true>, a complete block of
statements is to be executed; if <false>, an
alternate set of statements is to be executed.
• The IF(…)THEN-ELSE structure is a series of two
statement groups, one of which is executed
when a logical condition is met, otherwise the
second statement group will be executed.

Dr. Ali K. Abdel-Rahman


IF(…)THEN-ELSE Structure

IF ( <logical expression> ) THEN


This block of FORTRAN statements will be
executed only if the logical expression is
evaluated as true.
ELSE
This block of FORTRAN statements will be
executed only if the logical expression is
evaluated as false.
ENDIF

Dr. Ali K. Abdel-Rahman


IF(…)THEN-ELSE Structure

Dr. Ali K. Abdel-Rahman


IF(…)THEN-ELSE Structure
C**********************************************************************
PROGRAM QUAD
REAL A,B,C,DISCR,T1,T2,XMIN
PRINT*, ‘Enter the coefficients of the quadratic: a,b,c’
READ*, A,B,C
C
C Compute the discriminant
C -------------------------------------
DISCR = B*B – 4.0*A*C
C
C Test for complex roots
C --------------------------------
IF(DISCR .LT. 0.0) THEN
PRINT*, ‘Both roots are complex’
ELSE
C If not zero, define the two terms t1, t2
T1 = -B/2.0/A
T2 = SQRT(DISCR)/2.0/A
C
C The minimum root depends on the sign of b
C ---------------------------------------------------------------
IF(B .LT. 0.0) THEN
XMIN = T1 - T2
ELSE
XMIN = T1 + T2
ENDIF
PRINT*, ‘Smallest-magnitude real root = ‘,XMIN
ENDIF
STOP
END
C**********************************************************************

Dr. Ali K. Abdel-Rahman


ELSE IF Statement

• Frequently, the possible branches of a


computational algorithm are more numerous
than the two permitted in a true-false test.
• To accommodate these cases the ELSE IF
structure is used.
• The code for the roots of the quadratic may be
written using the ELSE IF structure in the
following way:

Dr. Ali K. Abdel-Rahman


Example: ELSE IF structure

READ*, A,B,C
DISCR = B**2 – 4.0*A*C
IF(DISCR .GT. 0.0) THEN
XXXXXX………………………
XXXXXX………………………
ELSE IF(DISCR .EQ. 0.0) THEN
XXXXXX……………………...
XXXXXX………………………
ELSE
XXXXXX……………... these statements are executed
XXXXXX……………... only if both IF tests fail
ENDIF

Dr. Ali K. Abdel-Rahman


ELSE IF Statement

Dr. Ali K. Abdel-Rahman


ELSE IF Statement
C**********************************************************************
PRINT*, ‘Enter test sides a, b, c’
READ*, A, B, C
PRINT*, ‘Sides a = ‘, A
PRINT*, ‘ b = ‘, B
PRINT*, ‘ c = ‘, C
C -------------- outer IF statements
IF((ABS(A-B) .LE. C) .AND. (C .LE. A+B)) THEN
PRINT*, ‘can indeed form a triangle that is’
C -------------- inner IF statement
IF(A .EQ. B) THEN
PRINT*, ‘ Isosceles’
C -------------- most inner IF statment
IF(B .EQ. C) THEN
PRINT*, ‘ and equilateral’
ENDIF
ELSEIF((A .EQ. C) .OR. (B. EQ. C)) THEN
PRINT*, ‘ Isosceles’
ELSE
PRINT*, ’ neither isosceles nor equilateral’
ENDIF
ELSE
PRINT*, ‘ can not form a triangle.’
ENDIF
C --------------- end of outer IF statement
STOP
END
C**********************************************************************
Dr. Ali K. Abdel-Rahman
Loop Structures in FORTRAN

• Perhaps the most common and useful


computational structure in programming is the
loop structure, wherein a block of statements is
executed and the block is then simply repeated
with some of the parameters changed.

Dr. Ali K. Abdel-Rahman


DO-Loop Structure

• The DO statement specifies the number of times


a sequence of statements will be executed.
• A DO-loop structure is a block of statements
with an entry point (the top) and a normal exit
point (the bottom).
• The beginning line of a DO-loop has the
following general form:

DO < labeI > INDX = ILO, IHI, ISTEP

Dr. Ali K. Abdel-Rahman


DO-Loop Structure

• All FORTRAN statements beginning with the DO


statement down to and including the DO
terminator (here statement <label>) are executed
first with the index INDX = ILO.
• The loop is then repeated with INDX = ILO+ISTEP
and so on until the maximum value is reached.

Dr. Ali K. Abdel-Rahman


DO-Loop Structure

• The terminal statement of the DO loop must be


an executable statement, but it cannot be any of
the following:
GO TO Unconditional
IF Arithmetic
ELSE
ELSEIF
STOP
END
DO

Dr. Ali K. Abdel-Rahman


DO-Loop Structure

Number of Cycles
DO Statement Executed Comments
DO 44 I = 1, 5 5
DO 73 K = 5, 1 0
DO 73 K = 5, 1, -1 5 Negative steps
DO 11 M = 1, 9, 3 3
DO 15 X = 1., 4., 0.8 4
DO 15 M = 1., 4., 0.8
Non (execution- The loop limits are
time error; zero first converted to the
step not allowed) type of M, so this
statement is the same
as DO 15 M=1,4,0
DO 91 R = SQRT(2.), 5 4

Dr. Ali K. Abdel-Rahman


Counting Loops

• The DO loop repeats an action a specific number


of times.
• In FORTRAN 77 do not need to have an INTEGER
data type, but use of integers is recommended if
you wish to maximize execution speed of your
program. Example:
L1 = 100
L2 = 200
L3 = 10
DO 250 I = L1, L2, L3

Dr. Ali K. Abdel-Rahman


Counting Loops

• Internally, the values of L1, L2 and L3 are used


to compute the number of repetitions of the DO
loop.
• The actual formula is (L2 – L1 + L3) / L3.
• Once the count is determined, it does not
change, even if the variables L1, L2, L3 are
assigned new values inside the loop.

Dr. Ali K. Abdel-Rahman


Counting Loops

• Keep in mind the following points when using


DO-loop structure:
– When exiting a DO loop before completion, the value of
the index parameter is the last value assigned.
– Never branch from outside a DO loop to statements
within a DO loop. The counter will most likely not have
an assigned value.
– Never redefine the DO-loop index within the loop.
Also, it is not possible to alter the DO-loop limits from
within the loop.
– To improve the readability of DO loops, indent the body
of the loop in a manner similar to block IF statements.

Dr. Ali K. Abdel-Rahman


Nested DO Loops

• When one DO loop is entirely contained within


another DO loop, the grouping is called a DO-
loop nesting.
• Nesting of DO loops to any depth is permitted,
provided each inner loop is entirely within the
range of the next-level outer loop.
• Basically, this rule ensures that the execution of
the inner loop is completed before moving on to
the next step of the outer loop.

Dr. Ali K. Abdel-Rahman


Nested DO Loops

• An inner loop and an outer loop may, however,


share a terminal statement.
• A few examples of allowable nesting are shown.

Dr. Ali K. Abdel-Rahman


Additional Control Statements
Unconditional GOTO

• The GOTO statement causes an unconditional


branch to another statement.
• An unconditional GOTO always sends control to
a specific statement.
• GOTO can be written GO TO, since FORTRAN
ignores spaces.

GOTO label
where label is the statement label of an
executable statement

• Example: 100 GOTO 9092

Dr. Ali K. Abdel-Rahman


Additional Control Statements
Computed GOTO

• The computed GOTO branches to one of several


locations within the program, depending on the
value of an INTEGER expression.

GOTO ( statement list ) [ , ] IntExp


where Statement list is a List of statement labels of
executable statements.
IntExp is an INTEGER expression. Its value
will determine which statement will be
executed next.

• Example: GOTO ( 690,100,300) INTEXP

Dr. Ali K. Abdel-Rahman


Additional Control Statements
Arithmetic IF Statement

• An arithmetic expression is evaluated. One of


three branches is selected, depending on the
value of the expression.

IF ( expression ) labe11, labe12, labe13


where expression Any arithmetic expression.
labe11 The label of an executable statement branched
to when the value of the expression is < 0.
labe12 The label of an executable statement branched
to when the value of the expression is = 0.
labe13 The label of an executable statement branched
to when the value of the expression is > 0.

Dr. Ali K. Abdel-Rahman


Additional Control Statements
Logical IF Test

• A statement is executed only when the logical


expression in the IF statement is evaluated to be
.TRUE.

IF ( logical expression ) statement


where
logical expression Can be any LOGICAL expression.
statement Can be any complete statement except a
DO statement, another IF statement or
END.

Dr. Ali K. Abdel-Rahman

You might also like