review of lecture 3 data type and declaration integer e.g., a, b, c real e.g., x, y, z, w logical...
TRANSCRIPT
![Page 1: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/1.jpg)
Review of lecture 3 Data type and declaration
INTEGER E.g., a, b, c
REAL E.g., x, y, z, w
LOGICAL COMPLEX CHARACTER
Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0
![Page 2: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/2.jpg)
Review of lecture 3 (cont.)
Arithmetic expressions E.g., 5 + 2 *3 E.g., b**2 – 4*a*c
It has a value by itself E.g. expression: 5 + 2*3 has a value of 11
![Page 3: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/3.jpg)
Review of lecture 3 (cont.)
Evaluating Complex Expressions Precedence Associativity (For expressions having operators with the
same precedence).
operators Precedence associativity
() 1 Left to right
** 2 Right to left
*, / 3 Left to right
+, - 4 Left to right
![Page 4: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/4.jpg)
Review of lecture 3 (cont.)
Mixed Mode Expressions If one operand of an arithmetic operator is INTEGER and the other is
REAL the INTEGER value is converted to REAL the operation is performed the result is REAL1 + 2.5 3.5 1/2.0 0.5 2.0/8 0.25 -3**2.0 -9.04.0**(1/2) 1.0 (since 1/2 0)3/5 * 5.0 0.0 (since 3/5 0)
![Page 5: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/5.jpg)
Review of lecture 3 (cont.)
Statements Assignment statement:
Variable = expression
e.g., b = -5
(never the other way around, 5=b is not valid!)
General statement:INTEGER a, b, c
write (*,*) ‘Hello world!’
![Page 6: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/6.jpg)
Selection in FORTRAN
Lecture 4
By Nathan Friedman and Yi Lin
Jan 16, 2007
![Page 7: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/7.jpg)
Roots of a Quadratic (again)! --------------------------------------------------! Solve Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0! --------------------------------------------------PROGRAM QuadraticEquation IMPLICIT NONE REAL :: a, b, c, d, root1, root2
! read in the coefficients a, b and c WRITE(*,*) "A, B, C Please : " READ(*,*) a, b, c
! compute the square root of discriminant d d = SQRT(b*b - 4.0*a*c)
! solve the equation root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root
! display the results WRITE(*,*) "Roots are ", root1, " and ", root2 END PROGRAM QuadraticEquation
![Page 8: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/8.jpg)
Run Time Error
We assumed that the discriminant was positive
What if it wasn’t?We would get an error when we run the program and
attempt to compute the square root and the program would abort
What should we do?Avoid trying to perform an illegal operation by branching
around it -- use a selection control structure
![Page 9: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/9.jpg)
Quadratics example
d=b*b – 4.0*a*c
D>=0.0
Calc root1, root2
Ouput “no real roots!”
.TRUE. .FALSE.
end
![Page 10: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/10.jpg)
IF-THEN-ELSE-END IF
Used to select between two alternative sequences of statements.
They keywords delineate the statement blocks.Syntax: IF (logical-expression) THEN first statement block, s_1 ELSE second statement block, s_2 END IF
![Page 11: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/11.jpg)
IF-THEN-ELSE-END IF
Semantics:• Evaluate the logical expression. It can have
value .TRUE. or value .FALSE.• If the value is .TRUE., evaluate s_1, the first block of
statements• If the value is .FALSE., evlaluate s_2, the second block
of statements• After finishing either s_1 or s_2, execute the statement
following the END IF
![Page 12: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/12.jpg)
Roots of Quadratic – v.2! ----------------------------------------------------------------! Solve Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0 ! ---------------------------------------------------------------- PROGRAM QuadraticEquation IMPLICIT NONE REAL :: a, b, c, d, root, root2 ! read in the coefficients a, b and c
WRITE(*,*) "A, B, C Please : " READ(*,*) a, b, c
!compute the square root of discriminant d d = b*b - 4.0*a*c IF (d >= 0.0) THEN ! is it solvable? d = SQRT(d) root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root WRITE(*,*) "Roots are ", root1, " and ", root2 ELSE ! complex roots WRITE(*,*) "There is no real roots!" WRITE(*,*) "Discriminant = ", d END IFEND PROGRAM QuadraticEquation
![Page 13: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/13.jpg)
Logical Expressions
Relational operators return result of .TRUE. or .FALSE<, <=, >, >=, ==, /=
Relational operators are of lower precedence than all arithmetic operators2 + 7 >= 3 * 3 .TRUE.
There is no associativitya < b < c illegal
Note that == means “is equal to” but = means “assign the value on the right”
![Page 14: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/14.jpg)
Data Type Logical
Where do .TRUE. and .FALSE. come from? FORTRAN has a LOGICAL data type, just like
it has INTEGER and REAL types We can declare variables to be of this type and
assign values to them
LOGICAL :: positive_x, condition
condition = .TRUE.
positive_x = x > 0
![Page 15: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/15.jpg)
Is Number Even or Odd?
IF (MOD(number, 2) == 0) THEN
WRITE(*,*) number, " is even"
ELSE
WRITE(*,*) number, " is odd"
END IF
MOD(a, b): Intrinsic function, returning the remainder of a/b
![Page 16: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/16.jpg)
Find Absolute Value
REAL :: x, absolute_x x = ..... IF (x >= 0.0) THEN absolute_x = x ELSE absolute_x = -x END IF WRITE(*,*) “The absolute value of “,& x, “ is “, absolute_x Note the use of & to indicate “continue on next line”
![Page 17: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/17.jpg)
Which value is smaller?
INTEGER :: a, b, min READ(*,*) a, b IF (a <= b) THEN min = a ELSE min = b END IF Write(*,*) “The smaller of “, a, & “ and “, b, “ is “, min
![Page 18: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/18.jpg)
IF-THEN-END IF
The IF-THEN-ELSE-END IF form allows us to choose between two alternatives
There is another simpler selection mechanism that allows us to choose whether or not to perform a single block of actions
We either perform the actions and go on, or skip them and go on
![Page 19: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/19.jpg)
IF-THEN-END IF
Syntax:IF (logical expression) THEN
block of statementsEND IF
Semantics:1. Evaluate the logical expression2. If it evaluates to .TRUE. execute the block of statements and
then continue with the statement following the END IF3. If the result is .FALSE. skip the block and continue with the
statement following the END IF
![Page 20: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/20.jpg)
Examples of IF-THEN-END IFabsolute_x = x IF (x < 0.0) THEN absolute_x = -x END IF WRITE(*,*) "The absolute value of ", x, " is ", absolute_x
------------------------------------------------- INTEGER :: a, b, min READ(*,*) a, b min = a IF (a > b) THEN min = b END IF Write(*,*) "The smaller of ", a, " and ", b, " is ", min
![Page 21: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/21.jpg)
Logical IF
An even simpler form is sometimes useful
Syntax:IF (logical expression) single-statement
Semantics: equivalent toIF (logical expression) THEN
single-statement
END IF
![Page 22: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/22.jpg)
Examples of Logical IFabsolute_x = x IF (x < 0.0) absolute_x = -x WRITE(*,*) "The absolute value of ", x, & " is" ,"absolute_x
------------------------------------------------- INTEGER :: a, b, min READ(*,*) a, b min = a IF (a > b) min = b Write(*,*) "The smaller of ", a, " and ", b, & " is ", min
![Page 23: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/23.jpg)
IF-THEN-ELSE IF-END IF
IF-THEN-ELSE-ENDIF can only handle processes which have two options.
Sometimes we have more than 2 options for one process.
For example, in the quadratic equation problem, there may be two equivalent roots. We want to detect this and complex roots at the same time.
Solution: put another IF-THEN-ELSE-ENDIF in the ELSE block
![Page 24: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/24.jpg)
IF-THEN-ELSE IF-END IF
SyntaxIF (logical-expression1) THEN
first statement block, s_1 ELSE
IF (logical-expression2) THEN second statement block, s_2 ELSE third statement block, s_3 END IFEND IF
![Page 25: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/25.jpg)
IF-THEN-ELSE IF-END IF
Semantics:• Evaluate the logical expression 1. It can have value .TRUE. or
value .FALSE.• If the value is .TRUE., evaluate s_1, the first block of statements• If the value is .FALSE.,
evaluate the logical expression 2. It can have value .TRUE. Or value .FALSE.
If the value is .TRUE., evaluate s_2, the second block of statements If the value is .FALSE., evaluate s_3, the third block of statements After finishing either s_2 or s_3, execute the statement following
ENDIF• After finishing either s_1 or s_2 or s_3, execute the statement
following the END IF
![Page 26: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/26.jpg)
Roots of Quadratic v.3! ---------------------------------------------------! Solve Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0 ! Detect complex roots and repeated roots.! ---------------------------------------------------PROGRAM QuadraticEquation IMPLICIT NONE
REAL :: a, b, c, d, root1, root2! read in the coefficients a, b and c READ(*,*) a, b, c
! comute the discriminant d d = b*b - 4.0*a*c IF (d > 0.0) THEN ! distinct roots? d = SQRT(d) root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root WRITE(*,*) 'Roots are ', root1, ' and ', root2 ELSE IF (d == 0.0) THEN ! repeated roots? WRITE(*,*) 'The repeated root is ', -b/(2.0*a) ELSE ! complex roots WRITE(*,*) 'There is no real roots!‘ WRITE(*,*) 'Discriminant = ', d END IF END IFEND PROGRAM QuadraticEquation
![Page 27: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/27.jpg)
IF-THEN-ELSE IF-END IF You can put IF-THEN-ELSE IF-ENDIF within ELSE block
repeatedly.IF (logical-expression1) THEN first statement block, s_1ELSE
IF (logical-expression2) THEN second statement block, s_2 ELSE IF-THEN-ELSE IF-ENDIF END IFEND IF
There is a concise way to do this.
![Page 28: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/28.jpg)
IF-ELSEIF-ELSE-ENDIF
syntaxIF (logical expression 1) THEN
block 1ELSEIF (logical expression 2) THEN
block 2 ... ELSEIF (logical expression N-1) THEN
block N-1[ELSE
block N] ENDIF
![Page 29: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/29.jpg)
IF-ELSEIF-ELSE-ENDIF
Semantics First evaluate the logical expression 1. It can have value .TRUE. or
value .FALSE. If the value is .TRUE., evaluate the first block of statements. If the value is .FALSE., evaluate the logical expression 2. It can have
value .TRUE. or value .FALSE. If the value is .TRUE., evaluate the second block of statements. Repeat this until all logical expressions have been evaluated. If none of them are .TRUE., evaluate block N within ELSE and
ENDIF. After that, execute the statement following the ENDIF
![Page 30: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/30.jpg)
Roots of Quadratic v.4---------------------------------------------------! Solve Ax^2 + Bx + C = 0 given B*B-4*A*C >= 0 ! Detect complex roots and repeated roots.! ---------------------------------------------------PROGRAM QuadraticEquation IMPLICIT NONE
REAL :: a, b, c, d, root1,root2! read in the coefficients a, b and c READ(*,*) a, b, c! compute the discriminant d
d = b*b - 4.0*a*c IF (d > 0.0) THEN ! distinct roots? d = SQRT(d) root1 = (-b + d)/(2.0*a) ! first root root2 = (-b - d)/(2.0*a) ! second root WRITE(*,*) 'Roots are ', root1, ' and ', root2 ELSEIF (d == 0.0) THEN ! repeated roots? WRITE(*,*) 'The repeated root is ', -b/(2.0*a) ELSE ! complex roots WRITE(*,*) 'There is no real roots!‘ WRITE(*,*) 'Discriminant = ', d END IFEND PROGRAM QuadraticEquation
![Page 31: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/31.jpg)
Example in previous midtermsPROGRAM X
IMPLICIT NONELOGICAL :: A,B,CINTEGER :: I,J,KI = 5/2+2.5J= 13.0/3.0 + 0.66K = 5A = (I==J)B = (K > J)C = (K==I)WRITE (*,*) A, B, C
END PROGRAM
1- T T T
2- T F T
3- T F F
4- F T F
5- None of the above
![Page 32: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/32.jpg)
Complex Logical Expressions
In addition to relational operators, more complex logical expressions can be formed using logical operators
The Logical Operators listed in order of decreasing precedence are:.NOT..AND..OR..EQV., .NEQV.
The precedence of all logical operators is lower than all relational operators
They all associate from left to right except .NOT.
High
Low
![Page 33: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/33.jpg)
Logical operator: .AND. (&&)
Examples (2>1) .and. (3<4) .TRUE. (2>1) .and. (3>4) .FALSE. (2<1) .and. (3<4) .FALSE. (2<1) .and. (3>4) .FALSE.
.TRUE. .FALSE.
.TRUE. .TRUE. .FALSE.
.FALSE. .FALSE. .FALSE.
<exp1> .AND. <exp2>
exp2exp1
<exp1> && <exp2>
.TRUE. .FALSE.
.FALSE. .FALSE.
![Page 34: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/34.jpg)
Logical operator: .OR. (||)
Examples (2>1) .OR. (3<4) .TRUE. (2>1) .OR. (3>4) .TRUE. (2<1) .OR. (3<4) .TRUE. (2<1) .OR.
(3>4) .FALSE.
.TRUE. .FALSE.
.TRUE. .TRUE. .TRUE.
.FALSE. .TRUE. .FALSE.
<exp1> .OR. <exp2>
exp2exp1
<exp1> || <exp2>
![Page 35: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/35.jpg)
Logical operator: .EQV.
Examples (2>1) .EQV. (3<4) .TRUE. (2>1) .EQV. (3>4) .FALSE. (2<1) .EQV. (3<4) .FALSE. (2<1) .EQV. (3>4) .TRUE.
.TRUE. .FALSE.
.TRUE. .TRUE. .FALSE.
.FALSE. .FALSE. .TRUE.
<exp1> .EQV. <exp2>
exp2exp1
![Page 36: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/36.jpg)
Logical operator: .NEQV.
Examples (2>1) .NEQV. (3<4) .FALSE. (2>1) . NEQV. (3>4) . TRUE. (2<1) . NEQV. (3<4) .TRUE. (2<1) . NEQV. (3>4) .FALSE.
.TRUE. .FALSE.
.TRUE. .FALSE. .TRUE.
.FALSE. .TRUE. .FALSE.
<exp1> .NEQV. <exp2>
exp2exp1
![Page 37: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/37.jpg)
Logical operator: .NOT.
Examples .NOT. (2<1) .TRUE. .NOT. (2>1) .FALSE. .TRUE. .FALSE.
.FALSE. .TRUE.
.NOT. <exp>
exp
![Page 38: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/38.jpg)
Examples
Suppose we have the declaration:INTEGER :: age=34, old=92, young=16
What is the value of the following expressions? age /= oldage >= youngage == 62 age==56 .and. old/=92 age==56 .or. old/=92 age==56 .or. .not.(old/=92).not. (age==56 .or. old/=92)
![Page 39: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/39.jpg)
Another Example
Suppose the integer variable, n has value 4. n**2 + 1 > 10 .AND. .NOT. n < 3
4**2 + 1 > 10 .AND. .NOT. 4 < 3 [4**2] + 1 > 10 .AND. .NOT. 4 < 3 16 + 1 > 10 .AND. .NOT. 4 < 3 [16 + 1] > 10 .AND. .NOT. 4 < 3 17 > 10 .AND. .NOT. 4 < 3 [17 > 10] .AND. .NOT. 4 < 3 .TRUE. .AND. .NOT. 4 < 3 .TRUE. .AND. .NOT. [4 < 3] .TRUE. .AND. .NOT. .FALSE. .TRUE. .AND. [.NOT. .FALSE.] .TRUE. .AND. .TRUE. .TRUE.
![Page 40: Review of lecture 3 Data type and declaration INTEGER E.g., a, b, c REAL E.g., x, y, z, w LOGICAL COMPLEX CHARACTER Examples: INTEGER::a=1,b=-5,c=5 REAL::x=2.0](https://reader035.vdocuments.net/reader035/viewer/2022062321/56649ec85503460f94bd59fb/html5/thumbnails/40.jpg)
Example in the final of Fall 2006What is the output of the following
Fortran program?PROGRAM exam IMPLICIT NONE LOGICAL :: X,Y INTEGER :: I=2, J=3 REAL :: A=5.0,B=9.0 X = (A/I) < (B/J) Y = (J/I) == (B/A) X = X .AND. Y Y = X .OR. Y WRITE (*,*) X, YEND PROGRAM exam
a). T Fb). T Tc). F Td). F Fe). The program does not
compile