Program
Structure
Data
Units
Assignment
Statements
Do
Loops
Do
While Statements
If Statements
Nested
If Statements
Logical
Assignment Statement
Read
and Write Statements
Format
Statements
Files
Data Types Declarations and Extent
: REAL
: INTEGER
Shared Memory Definitions
: COMMON
: EQUIVALENCE
Body of Program
Assignment Statements
READ/WRITE Statements
Arithmetic Operations
etc.
Subroutine 1 - does a specific task
Subroutine 2 - does a specific task
:
:
END
In a program, the author must include the following at the beginning
of the program:
Bit: 1 or 0
Byte: 8 bits
Word: 2 bytes or longer, machine dependent
Data Types:
Real 4 bytes (exponential, real)
Integer 4 bytes (-2147483647 to +2147483648)
Complex 8 bytes (complex number, real and imaginary parts)
Logical 4 bytes (.true. or. false.)
Character 1 byte/character (file names, strings)
Size can be redefined: Real*8 real variable of 8 bytes
Assignment
Statements:
Assignment statements are for the purpose of defining variables or
giving them values.
Examples:
X = 1. Real variable
IX = 1 Integer variable
XC = CMPLX (1.0, 3.0) Complex variable
X = 1.E6 Real variable
Sun Text Editor: The Sun editor can be accessed by TEXTEDIT <file name>. It is a click and point editor.
Do Loops:Example:
DO J = NBEGIN, NEND, INC
:
:
:
END DO
NBEGIN is the initial value for J.
NEND is the value of J when the loop is to end.
INC is the increment in J from NBEGIN to NEND. INC can
be positive or negative, but never zero. The default value
for INC is 1.
Example: unformatted WRITE and simple DO loop
IMPLICIT NONE (A,Z)
INTEGER I
DO I=1,100
DO J=1,I
WRITE(*,*)
I,J
END DO
END DO
STOP
END
How many times will the inner loop execute?
This code should be tested by the students in lab.
DO WHILE Statement:Example:
DO WHILE (logical expression)
:
:
END DO
What is the difference between Do while and do loop
Things to avoid in Loops:
1) Invariants
Example:
DO J = 1, N
X = 1.
Y = 2.
END DO
2) Changing the value of the counter, J, cannot be done in a loop. The
compiler will not allow it.
3) Performing simultaneous operations from both ends of an array.
Example:
DO J = 1, N
K = N - J + 1
Y (J) = X (J) + X (K)
END DO
We can instead use two different loops.
DO J = 1, N
Y (J) = X (J)
END DO
DO J = 1, N
K = N-J+1
Y (J) = Y (J) + X (K)
END DO
1) Arithmetic IF Statements (these are currently not used but are still supported and found in many vintage programs). In this case, the IF statement is followed by a sequence of 3 statement numbers. The statement will branch to one of the 3 statement numbers depending on the result of the parenthetical expression.
Example:
IF ( detm ) a, b, c
If the result within the parenthesis (variable detm) is:
< 0, then the program will go to statement number a.
= 0, then the program will go to statement number b.
> 0, then the program will go to statement number c.
2) Logical IF Statements (these are used for structured FORTRAN). In this case, the IF statement is followed by an operation that is to be carried out if the result of the logical expression within parenthesis is true.
Examples:
IF (A. EQ. B) C = D
IF (B .GT. L) R = L - B
Fortran Logical Operators Are:
.EQ.
.NE.
.GT.
.GE.
.LT.
.LE.
.OR.
.AND.
Types of Logical IF Statements:
1) IF ( ) one statement
This IF statement stands alone. If the logical expression within the parenthesis is true, then the statement following it is executed.
2) IF ( ) THEN
statement 1
statement 2
ELSE
statement 3
statement 4
etc
ENDIF
The above IF-ELSE-ENDIF-construct performs the sequence of instructions starting with statement 1 if the logical expression within the parenthesis is true. If it is false, the program skips down and performs the sequence of instructions starting with statement 3. The ELSE and initial group of statements are optional:
IF ( ) THEN
statement 1
statement 2
etc
ENDIF
( Example)
IF ( ) THEN
statement 1
statement 2
IF ( ) THEN
statement 3
statement 4
ENDIF
ENDIF
IF ( ) THEN
statement 1
ELSE IF( ) THEN
statement 2
ELSE
statement3
ENDIF
ENDIF
In case of nested if else statement the else corresponds to the nearest
if.
Example:
LOGICAL ISTRUE, SAVE
ISTRUE = A .EQ. 3
ISTRUE = A .GT. B
ISTRUE = .NOT. (A .LT. C .OR. B .GT. C)
IF (ISTRUE) THEN
:
:
:
END IF
IF (ISTRUE .EQV. SAVE) THEN
:
:
END IF
IF (ISTRUE .NEQV. SAVE) THEN
:
:
END IF
READ statements are used to input data from a file or the keyboard into the program. WRITE statements are used to output information from the program to a file or the terminal.
Examples:
READ (LUN, NFMT) VAR1, VAR2
WRITE (LUN, NFMT) VAR1, VAR2
where:
LUN is the Logical Unit Number (LUN). It is an integer number that
represents a file or the terminal. (When LUN = 5, the program inputs data
from the terminal.)
NFMT is either an integer number that specifies the FORMAT statement
to be used, or it can be the character *. If it is the character *, then
the program performs a free format read or write, adjusting the data to
the data type specified in the variable list.
VAR1, VAR2 is an example of the variable list.
Format statements are used to position and define the type of input or output (I/O) from your program. Each FORMAT statement contains a statement number and a coded list of the format whereby the variables will be read in or written out.
An example of a FORMAT statement:
WRITE (LUN, 10020) I, Y
10020 FORMAT (1X, I6/,F10.2)
Here the FORMAT statement specifies:
Format # - Defines the FORMAT statement.
1X - Skips one column before writing.
I6 - Writes a 6 column integer.
/ - issues a line feed (new line)
F10.2 - Writes a real number that takes up 10 columns. 7 digits will
be to the left of the decimal place, and two will be to the right.
The decimal place takes up one digit.
These are basic entities within the computer that contain programs, subroutines, data, etc. There are several basic commands besides READ and WRITE used to manipulate these files.
1) OPEN: The open statement associates a file name with a Logical Unit Number (LUN), so that the file can be accessed with READ and WRITE statements.
Example:
OPEN (LUN, NAME = 'GEO.DAT', STATUS = 'NEW')
LUN is the Logical Unit Number. This is an integer number that
is associated with the filename for use with READ and WRITE statements.
NAME specifies the filename to be associated with the LUN. Here
the name of the file is GEO.DAT.
STATUS specifies whether the file is 'NEW' or 'OLD'. (i.e. If the file
does not exist, use 'NEW.' If you want to open a pre-existing file,
use STATUS = 'OLD'). If you don’t know or don’t care use STATUS =
‘UNKNOWN’.
2) CLOSE: The CLOSE statement disassociates a filename from a Logical Unit Number. It can also be used to delete a file.
Example:
CLOSE (LUN)
CLOSE (LUN, STATUS = ‘DELETE’)
3) BACKSPACE: Go to the beginning of current record.
4) REWIND: Go to the beginning of the file.