FORTRAN 90 Lecture : Rafel Hekmat Hameed University of Babylon Subject : Fortran 90 College of Engineering Year : Second B.Sc Mechanical Engineering Dep. 1. INTEGER DIVISION 10 / 3 evaluates to 3 19 / 4 evaluates to 4 4 / 5 evaluates to 0 (which could cause an unwanted division by zero) - 8 / 3 evaluates to -2 3 * 10 / 3 evaluates to 10 10 / 3 * 3 evaluates to 9 2. Mixed-mode expressions 10 / 3.0 evaluates to 3.33333 4. / 5 evaluates to 0.8 2 ** (- 2) evaluates to 0 (?) However, note that 3 / 2 / 3.0 Evaluates to 0.333333 because 3 / 2 is evaluated first, giving integer 1. 3. Numeric Assignment If expr is not of the same type as var, it is converted to that type before assignment. This means that there might be loss of precision. For example ,assuming N is integer, and X and Y are real: N = 10. / 3 (value of N is 3) X = 10 / 3 (value of X is 3.0) Y = 10 / 3. (value of Y is 3.33333) ϭ 4. Flowchart A flowchart is a type of diagram that represents an algorithm or process, showing the steps as boxes of various kinds, and their order by connecting these with arrows. Flowchart symbol Symbol Description Start or stop point in process. An operation or action step. A question or branch in the process. Indicates data inputs and outputs to and from process. A preparation or set-up process step. Connector a jump from one point to another. Continuation onto another page. Indicated the direction of flow for materials and/or information. Flowchart for computing the factorial N!=(1234567..10) represented below Ϯ of N (10!) where start Read N Dсϭ &сϭ F=F. M DсDнϭ No IS M=N ? Yes PRINT F End 5.Relational Operators There are six relational operators: <: <= : >: >= : == : /= : .LT. .LE. .GT. .GE. .EQ. .NE. less than less than or equal to greater than greater than or equal to equal to not equal to Important rules: Each of these six relational operators takes two operands. These two operands must both be arithmetic or both be strings. For arithmetic operands, if they are of different types (i.e., one INTEGER and the other REAL), the INTEGER operand will be converted to REAL. ϯ The outcome of a comparison is a LOGICAL value. For example, 5 /= 3 is .TRUE. and 7 + 3 >= 20 is .FALSE. All relational operators have equal priority and are lower than those of arithmetic's operators. Example a + b /= c*c + d*d Expressions a+b and c*c + d*d are evaluated before the relational operator /= is evaluated. Examples 3**2 + 4**2 == 5**2 is .TRUE. If the values of REAL variables a, b and c are 1.0, 2.0 and 4.0, respectively, then b*b - 4.0*a*c >= 0.0 is equivalent to 2.0*2.0 - 4.0*1.0*4.0 >= 0.0, which evaluates to -12.0 >= 0.0. Thus, the result is .FALSE. If REAL variables x and y have values 3.0 and 7.0, and INTEGER variables p and q have values 6 and 2, what is the result of x*x - y*y + 2.0*x*y /= p*q + p**3 - q**3? 6. LOGICAL Operators and Expressions Fortran has five LOGICAL operators that can only be used with expressions whose results are logical values (i.e., .TRUE. or .FALSE.). All LOGICAL operators have priorities lower than arithmetic and relational #_> #_> operators. Therefore, if an expression involving arithmetic, relational and logical operators, the arithmetic operators are evaluated first, followed by the relational operators, followed by the logical operators. These five logical operators are .NOT. .AND. .OR. .EQV. .NEQV. : logical not : logical and : logical or : logical equivalence : logical not equivalence ϰ The priority of .NOT. is the highest, followed by .AND., followed by .OR., followed by .EQV. and .NEQV. Note that .NOT. is right associative, while the other four are left associative. Examples Let LOGICAL variables Something and Another have values .TRUE. and .FALSE., respectively. .NOT. Something .AND. Another --> .NOT. .TRUE. .AND. .FALSE. --> [.NOT. .TRUE.] .AND. .FALSE. --> .FALSE. .AND. .FALSE. --> .FALSE. Let LOGICAL variables a, b and c have values .TRUE., .TRUE. and .FALSE., respectively. .NOT. a .OR. .NOT. b .AND. c --> .NOT. .TRUE. .OR. .NOT. .TRUE. .AND. .FALSE. --> [.NOT. .TRUE.] .OR. .NOT. .TRUE. .AND. .FALSE. --> .FALSE. .OR. .NOT. .TRUE. .AND. .FALSE. --> .FALSE. .OR. [.NOT. .TRUE.] .AND. .FALSE. --> .FALSE. .OR. .FALSE. .AND. .FALSE. --> .FALSE. .OR. [.FALSE. .AND. .FALSE.] --> .FALSE. .OR. .FALSE. --> .FALSE. Let INTEGER variables m, n, x and y have values 3, 5, 4 and 2, respectively, what is the result of the following expression? .NOT. (m > n .AND. x < y) .NEQV. (m <= n .AND. x >= y) ϱ