(4) Selection In Fortran (Sina)

(4) Selection In Fortran (Sina) - Selection in Fortran Sina...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Selection in Fortran Sina meraji Fall, 2009 Today ! Review ! READ statement ! IMPLICIT NONE ! RELATIONAL operators ! Selection (IF-THEN) Review Variable Data Types: •INTEGER •REAL •LOGICAL •COMPLEX •CHARACTER Variable precision: 101101 11 001011 11 111001 11 1 01110 10 INTEGER Default size = 4 bytes (KIND=4) Sign bit 1: negative 0: positive Review INTEGER :: X X = 1/2->X = 0 REAL :: X X = 1/2-> X = ?? Truncation: 1/2 ! 0.5 INTEGER :: X X = 1/2->X = 0 REAL :: X X = 1/2-> X = ?? INTEGER :: X X = 1/2->X = 0 REAL :: X X = 1/2-> X = ?? Review INTEGER :: X X = 1/2->X = 0 REAL :: X X = 1/2-> STILL X = 0 !! QuickTime™ and a decompressor are needed to see this picture. Truncation: 1/2 ! 0.5 Review INTEGER :: X X = 1/2->X = 0 REAL :: X X = 1/2-> STILL X = 0 !! REAL :: X X = 1.0 / 2.0-> X = 0.5 Truncation: 1/2 ! 0.5 Review +, - *, / ^ () operators Precedence 4 3 2 1 +, - *, / ^ () operators Associativity Left to right Left to right Right to left Left to right 100 + (1 + 250 / 100) ^ 3 = 127 The READ Statement ! Syntax: READ (*,*) var 1 , var 2 , . . ., var n ! Example ! READ (*,*) Celsius !From Elmer Fudd program ! Semantics: " Start a new line that contains the input " Input values must be the same type as the corresponding variables (e.g. you can't put a real number into an integer) " Data must be separated by commas or blanks " Extra input values on that line are ignored What is this “IMPLICIT-NONE”? ! In Fortran, you can use variables without declaring them ! All the variables started with I, J, K, L, M and N are Integer ! All the others Real What is this “IMPLICIT-NONE”? ! Writing Implicit-None at the beginning of the program tells the computer to point out errors if you try to use a variable that doesn't exist. ! For example, if I write “Celcius” (instead of “Celsius”) by accident, it will give me an error when I compile ! If we write “Celcius” instead of “Celsius” most likely we made a mistake that will matter later on in our program. ! While it is good that the program compiles, the program will not work correctly and we'll have to find the mistake by hand! Another simple example: Roots finding ! Finding roots for quadratic aX 2 + bX + c = 0 ! Input a, b, c ! Output root(s): ( -b + SQRT(b*b - 4*a*c) ) / (2*a) ( -b - SQRT(b*b - 4*a*c) ) / (2*a) Roots finding (cont.) start Initialize a=1, b=-5, c=6 x = (b*b – 4*a*c) R1 = (-b + sqrt(x)) / (2*a) R2 = (-b - sqrt(x)) / (2*a) End PROGRAM RootFinding IMPLICIT NONE INTEGER a, b, c REAL x, y, z, w, r1, r2 a = 1 b = 5 c = 8 !To calculate b * b - 4 * a *c x = b*b y = a*c z = 4*y w = x - z r1 = (-b + SQRT(w) / (2*a)) r2 = (-b - SQRT(w) / (2*a)) WRITE (*,*) r1, r2 END PROGRAM...
View Full Document

This note was uploaded on 01/15/2010 for the course COMP COMP 206 taught by Professor Vybihal during the Spring '04 term at McGill.

Page1 / 37

(4) Selection In Fortran (Sina) - Selection in Fortran Sina...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online