Programming with FORTRAN (In a nut shell) FORTRAN character set Alphabets: a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x, y, z Digits: 0 1 2 3 4 5 6 7 8 9 Symbols: + - * / . , ‘ _ ( ) FORTRAN constants Two types: Character constant, Numeric constants Numeric constants: Integer and Real FORTRAN variables Two types: Character variable, Numeric variables Numeric variables: Integer and Real Declaration: INTEGER list of variables REAL list of variables IMPLICIT type (…….) Library functions Common functions Trigonometric functions Inverse Trigonometric functions Hyperbolic functions sqrt(x) exp(x) log(x) log10(x) abs(x) sin(x) cos(x) tan(x) asin(x) acos(x) atan2(x,y) sinh(x) cosh(x) tanh(x) x: in radian FORTRAN coding sheet • • • • • Coding sheet is a paper with 80 columns If the character C or ! is written in the first column, the line is treated as a comment line and it is not read by the computer Statements are written starting from 7th column and up to 72th column If one statement is continued in the next line, then a character must put at the sixth column of the later line Column 1-5 are used for giving line numbers in FORTRAN Data files opening a file: open (no, file =’name’,status=’old’) ending a file: endfile m m is the file number Read Statement read(n,m) v1,v2,… n: input file number (* for reading from the screen) m: input data format (* for free format) v1,v2,… : input arguments Write Statement write(n,m) v1,v2,… n: output file number (* for writing at the screen) m: output data format (* for free format) v1,v2,… : input arguments Formatting of data I format (For Integer data): Iw (w is the width of the integer data) F format (For real data): Fw.d (w is the total width including sign, dot and decimal point; d is the number of decimal point) E format (For real data): Ew.d (w is the total width including sign, dot and decimal point; d is the number of decimal point) X format (to skip some columns): nX (n is the number of columns to be skipped) / format (to skip to the next line) Control statements GO TO statements 1. Unconditional go to statement: go to n (n is the statement number) 2. Computed go to statement: go to (n1,n2,…), i (n1,n2,… are the statement numbers and i is an integer) If i =1, go to n1, if i =2, go to n2 … IF statements 1. Arithmetic IF statement: If (expression) n1, n2, n3 n1 is for negative, n2 is for zero and n3 is for positive 2. Conditional IF statement: If (condition) statement < .LT. > .GT. <= .LE. >= .GE. = .EQ. .NE. Moreover .AND. , .OR. May be used 3. IF THEN ELSE statement: Type1: If (condition) then s1 s2 . . . endif Type2: If (condition) then s1 s2 . . . else s1’ s2’ . . . endif 4 Nested IF THEN ELSE statement: IF THEN ELSE statement within one or more IF THEN ELSE statement not crossing each other DO loops do I v=v1, v2, v3 …………….. …………….. I continue i.e. I is the last statement in the DO loop v is the running variables (integer/real); v1 is the initial value (integer/real); v2 is the final value (integer/real); v3 is the increment (integer/real) Rules: • • • • • • • • • • The statement number I must be a valid statement number which occurs after the DO statement i.e. the last statement. There must be one blank space each before and after the line number v is an unsigned valid unsigned integer or real variables v1,v2,v3 are valid variables, constants or expressions (integer or real) If v3 is omitted, it is assumed to be 1 The value of the running variable should not be altered within the loop Control cannot be transferred from a statement outside the DO loop to statement inside the DO loop Control should not be transferred from a statement with in the DO loop to the do statement itself Control cannot be transferred from a statement within the DO loop to statement outside the DO loop The last statement of a DO loop should not be a control statement, i.e. the last statement should not be a go to statement, if statement or another DO loop. Nested do loops: When one DO loop is constructed within another do loop, it is called a nested do loop. Rules: • • • DO loops should not overlap In case of nested DO loop, the same statement can be the last statement for more than one DO loop The running variable name of the outer loop should not be used as running variable for an inner nested DO loop Array If A is a sub scripted variable then: dimension A (array length) For multi dimensional sub scripted variables: dimension A (array length1, array length 2, ...)