CSCE Syntax Details Fortran is not case sensitive; any use of capitals is for the programmer’s convenience. All programs are “wrapped” with PROGRAM program_name END PROGRAM program_name
CSCE Syntax Details Symbols/Identifiers Must start with an alpha character Must be alphanumeric or underscore characters Must be lessequal 31 characters long
CSCE Syntax Details Declaration of variables IMPLICIT none should always be used REAL :: list of real variables INTEGER :: list of integer variables LOGICAL :: list of logical variables COMPLEX :: list of complex variables CHARACTER :: list of char variables CHARACTER(nn) :: list of char variables CHARACTER(LEN=nn) :: list of char variables
CSCE Syntax Details REAL,PARAMETER :: x = value Makes the variable a constant whose value cannot be changed from that originally assigned to it
CSCE Syntax Details—Arithmetic Expressions Exponentiations first, left to right, with multiple exponentiations done right to left Then mults and divs left to right with equal priority Then adds and subs left to right with equal priority Must include all operators between operands Cannot use multiple + or – signs in sequence DO NOT RELY ON THE RULES PARENTHESISE FOR CLARITY
CSCE Syntax Details—Arithmetic Expressions Detailed rules exist for arithmetic evaluation Ignore the rules Parenthesise as needed to ensure clarity, and use the builtin functions INT(), REAL(), etc., so that there is no question as to what you intend to be the result of your expression.
CSCE Syntax Details--Assignments Written var = expression but interpreted var expression Continuation to line n+1 indicated with & at the end of line n Up to 132 characters per line, up to 39 continuation lines permitted for a single statement
CSCE Input and Output PRINT *, list of variables WRITE(6,*) list of variables WRITE(6,nn) list of variables nnFORMAT(format statement here) The first two are “free format” and the last pair is a specifically formatted output statement By default, “unit 5” is stdin for reading and “unit 6” is stdout for writing READ *, list of variables READ(6,*) list of variables
CSCE Input and Output from Files OPEN(UNIT=n,FILE=“filename”,STATUS=“OLD”) READ(n,*) list of variables CLOSE(n) will allow reading from an external file filename Similarly, for writing, we would use OPEN(UNIT=n,FILE=“filename”,STATUS=“NEW”) WRITE(n,*) list of variables CLOSE(n) To open/create a file filename and write to it Format statements can also be used
CSCE Logical Expressions.EQ..NE..LT..GT..LE..GE..AND..OR..NOT..TRUE..FALSE. Rules of Boolean logic apply Precedence rules apply, but can be ignored if you remember to parenthesise properly
CSCE If-Then-Else IF(logical expression) THEN sequence of statements END IF label: IF(logical expression) THEN sequence of statements END IF label The labelling can be used on all IF statement structures
CSCE If-Then-Else IF(logical expression) THEN sequence of statements ELSE sequence of statements END IF
CSCE If-Then-Else IF(logical expression) THEN sequence of statements ELSE IF (logical expression) THEN sequence of statements. ELSE sequence of statements END IF
CSCE If-Then-Else SELECT CASE (selector variable) CASE(values) sequence of statements CASE(values) sequence of statements. CASE DEFAULT sequence of statements END SELECT
CSCE The End