04Choices-handout

04Choices-handout - Selection in Fortran Amir Hossein...

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

View Full Document Right Arrow Icon
Selection in Fortran Amir Hossein Rabbani Winter, 2009
Background image of page 1

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

View Full DocumentRight Arrow Icon
Today Review READ statement IMPLICIT NONE RELATIONAL operators Selection (IF-THEN)
Background image of page 2
Review Variable Data Types: INTEGER •REAL •LOGICAL COMPLEX •CHARACTER Variable precision: This is for INTEGER, to know how to use KIND parameter for other data types refer to the sources introduced in the class 1 0111010 11100111 00101111 10110111 INTEGER Default size = 4 bytes (KIND=4) Sign bit 1: negative 0: positive
Background image of page 3

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

View Full DocumentRight Arrow Icon
Review INTEGER :: X X = 1/2 ->X = 0 REAL :: X X = 1/2 -> X = ?? Truncation: 1/2 0.5
Background image of page 4
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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
Review operators Precedence () 1 ** 2 *, / 3 +, - 4 operators Associativity () Left to right ** Right to left *, / Left to right +, - Left to right 100 + (1 + 250 / 100) ** 3 = 127
Background image of page 7

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

View Full DocumentRight Arrow Icon
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
Background image of page 8
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!
Background image of page 9

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

View Full DocumentRight Arrow Icon
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)
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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 RootFinding Declare variables, Must be at the head of block initialize variables with values statements: calculation statements:Output results
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the 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 / 35

04Choices-handout - Selection in Fortran Amir Hossein...

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

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