2. Selective Execution
Selective execution gives the programmer the
ability to execute a part of the code if a
condition is met. Else that part of code is not
executed.
The selection is based on a logical
expression.
3. Logical Expressions
Logical expressions may be simple or
compound.
Simple logical expression
expression1 logical operator expression2
Compound logical expression
formed by combining simple logical
expressions by using the following logical
operators: NOT, AND, OR, EQV, NEQV.
5.
The result of a logical operation is either
“true” or “false”.
5>4
9<6
3>3
true.
false.
false.
6. Characters & Relational Operators
Numeric codes are used to establish an
ordering for the character set.
ASCII & EBCDIC “Refer to App. A”
“CAT” < “DOG” true C precedes D in the table.
“June” < “July” false l precedes n in the table.
“June” < “june” true J precedes j in the table.
Two strings are compared as though blanks are appended to the
shorter string.
“cat” < “cattle” true a blank space precedes t in the table.
7.
Logical expressions formed by comparing real values
with == are often evaluated as false, why?
because real values are stored with some error.
Real:: value
value = 10
if ( (1/value) == 0.1) then
do something
end if
In this example (1/value) might not result in 0.1 due to
storage limitations, it might be 0.998 or 0.10001
8. Declaring logical variables
There are two logical constants in Fortran
.TRUE. .FALSE.
Declaration:
LOGICAL :: VARIABLE_LIST
Reading logical variables is similar to reading other
data types.
Ex. Logical :: A,B,C
Read *, A,B,C
Input : .T. , F, .False “any form is valid input”
12. Priorities
In logical expression containing several of
these operators, the operations are
performed in the following order
.NOT. .AND. .OR. .EQV. (or .NEQV.)
Parenthesis may be used to indicate the subexpressions that must be evaluated first.
Ex:
A .AND. B .OR. .NOT. C
A .AND. (B .OR. .NOT. C)
13. The general case
The most general case is to have logical expressions
containing arithmetic, relational and logical operators.
The operations are performed in the following order:
1. Arithmetic operations
2. Relational operations
3. Logical operations
Ex: (((b**2 – 4* a* c) >=5) .and. c+2 <6)
14. Flowchart Symbols
Begin or End
Input or Output
Assignment
Point of Selection
Multiway Selection
Flow Lines join
15. IF Construct
IF (Logical Expression) then
Statement-Sequence
End IF
Selection is made between
either executing the sequence
of statements or bypassing it.
if
Stat. seq
16. General form of IF Construct
IF (Logical Expression) then
Statement-Sequence1
ELSE
Statement-Sequence2
End IF
Selection is made between
either executing sequence1
or sequence2.
if
Stat. seq1
Stat. seq2
17. Nested IF
The sequence of statements in an IF construct may
itself contain other IF constructs.
Ex: if(X<=0) then
value = -X
else
if (X<1.0)then
value = X**2
else
value=1.0
end if
end if
18. IF
ELSE IF Construct
IF(logical expression1) then
statement sequence 1
ELSE IF (logical expression2) then
statement sequence 2
ELSE IF (logical expression3) then
statement sequence 3
.
.
.
ELSE
statement sequence n
END IF
19. IF
ELSE IF Construct
This structure is used in cases involving
multiple alternatives.
Only one of the statement sequences will be
executed, the others will be skipped.
Notas del editor
always use parenthesis to control the order of execution.