FortranLecture #4 - Selective Exectuon (IF)

advertisement
1
Lecture 4
Selective Execution (IF statements)
In many applications, it is desired to control the execution of the program
based on certain criteria: an input value, a value of a variable, if a values
is greater than another value, etc. We can do this with IF statements and
logical expressions.
Logical Expressions
logical-expressions take the form
expression
relational-operator
expression
where expression is any expression that we can define (recall, whatever
we can place on the right side of an "=" in an assignment statement is by
definition an expression). The result of evaluating a logical expression is
either: .TRUE. or .FALSE.
2
Lecture 4
We need to define some relational-operators:
Symbol
<
or .LT.
>
or .GT.
= = or .EQ.
<= or .LE.
>= or .GE.
/= or .NE.
meaning
is less than
is greater than
is equal to
is less than or equal to
is greater than or equal to
is not equal to
We also have some logical operators (there are some more – see p. 46)
.NOT.
.AND.
.OR.
negation
conjunction
disjunction
With these operators, we can define several IF statements to control the
execution of a computer program.
3
Lecture 4
The simplest IF statement:
IF (logical-expression) executable statement
For example,
IF (I .LT. 0) A5 = 6.2
IF (I < 0) A5 = 6.2
IF (A >= 1.0) PRINT *, I, J, A
We can use the logical operators to make the test more restrictive. For
example,
IF (3 <= A .AND. B .GT. 5.2) PRINT *, I, VELOCITY
We can use parenthesis to nest logical expressions or to simply make
them easier to read:
IF ((3 <= A) .AND. (B .GT. 5.2)) PRINT *, I, VELOCITY
IF ( (I = = 1) .AND. ( (J = = 0) .OR. (K .GE. 5) ) ) X = X + 1.0
4
Lecture 4
The Simple IF statement: IF THEN
The general form of the IF THEN statement construct is given by
IF (logical-expression) THEN
Statements to be executed
END IF
More statements to be executed
If the logical-expression is .TRUE. , then the statements directly below
the IF are executed. If the statement is .FALSE. , then execution passes
to the statements directly below the END IF statement.
5
Lecture 4
Calculate the roots of a quadratic: A x2 + B x + C = 0. Solution is given
 B  B 2  4 AC
by the quadratic formula: x 
.
2A
Read *, A, B, C
Test=B**2-4.0*A*C
IF (Test.GT.0.) THEN
X1=(-B+SQRT(Test))/(2.0*A)
X2=(-B-SQRT(Test))/(2.0*A)
PRINT *, A,B,C,X1,X2
STOP
END IF
PRINT *, "Roots are complex"
STOP
Note: in the above, the IF can be written
IF (Test > 0.0) THEN
6
Lecture 4
IF THEN ELSE
The general form of the IF THEN statement construct is given by
IF (logical-expression) THEN
Statement group #1 to be executed
 executed only if
logical-expression is
.TRUE.
ELSE
Statement group #2 to be executed
END IF
More statements to be executed
 executed only if
logical-expression is
.FALSE.
7
Lecture 4
Example:
IF (I > 0) THEN
X=A+2.0
Y=X**2 – SQRT (C)
ELSE
X=A-2.0
Y=X**2 + SQRT (C)
END IF
PRINT *, “for i= “, I, A, C, X, Y
STOP
Note: Indenting the statements as shown above helps to identify each
statement group (makes the program easier to read).
8
Lecture 4
The IF THEN ELSE statements may be nested. For example,
IF (I > 0) THEN
X=A+2.0
! done when I>0
Y=X**2 – SQRT (C)
“
ELSE
IF (I < 0) THEN
X=A-2.0
! done when I<0
Y=X**2 + SQRT (C)
“
ELSE
Print *, “I is equal to zero, program will stop.” ! I=0 only
STOP
END IF
END IF
PRINT *, I, A, C, X, Y
STOP
9
Lecture 4
IF ELSE IF
The IF ELSE IF construct is similar to the nested IF THEN ELSE
example above. You may see it used and if you like it, then use it.. For a
three alternative structure, the coding looks like the following:
IF (logical-expression1) THEN
Statement group #1 to be executed
ELSE IF (logical-expression2) THEN
Statement group #2 to be executed
ELSE
Statement group #3 to be executed
END IF
More statements to be executed
The logical expressions are evaluated to determine the first expression
that is true; then the associated group of statements is executed (the
statements directly below the true IF). Execution then passes to the
statements directly below the ELSE or END IF.
10
Lecture 4
An example (same as above example but written with IF ELSE IF
construct):
IF (I > 0) THEN
X=A+2.0
! done when I>0
Y=X**2 – SQRT (C)
“
ELSE IF (I < 0) THEN
X=A-2.0
! done when I<0
Y=X**2 + SQRT (C)
“
ELSE
Print *, “I is equal to zero, program will stop.” ! I=0 only
STOP
END IF
PRINT *, I, A, C, X, Y
STOP
Looks a little different than previous example, but does exactly the same
thing; may be easier to read (you decide).
11
Lecture 4
The IF ELSE IF may have many decision points as shown below:
IF (logical-expression1) THEN
Statement group #1 to be executed
ELSE IF (logical-expression2) THEN
Statement group #2 to be executed
ELSE IF (logical-expression3) THEN
Statement group #3 to be executed
ELSE IF (logical-expression4) THEN
Statement group #4 to be executed
ELSE
Statement group #5 to be executed
END IF
More statements to be executed
The only statement group executed is that group directly below the
logical expression that is true. If none of the logical expressions are true,
then the statement group below the ELSE is executed. After this,
execution returns to the statements directly below the END IF.
12
Lecture 4
The Arithmetic IF
The general form of the arithmetic IF is given by
IF (arithmetic-expression) statement no. 1, statement no. 2, statement no. 3
Depending upon the sign of the value of the arithmetic expression,
control is passed to another statement as follows:
Arithmetic expression
Negative
Zero
Positive
execution transferred to
statement no. 1
statement no. 2
statement no. 3
This was an often used IF statement in early FORTRAN. Fortran 90
purists consider it archaic - you be the judge.
Lecture 4
The square root example:
Read *, A, B, C
Test=B**2-4.0*A*C
IF (Test) 10, 20, 20
20 X1=(-B+SQRT(Test))/(2.0*A)
X2=(-B-SQRT(Test))/(2.0*A)
PRINT *, A,B,C,X1,X2
STOP
10 PRINT *, "Roots are complex"
STOP
13
Download