c2 - TAMU Computer Science Student Pages

advertisement
]CS 203 – Introduction to Computing
Chapter 2 – Basic Elements of Fortran
2.2
The Fortran Character Set
(See Table 2-1)
Note that Fortran Codes are “Case Insensitive” to the Compiler.
2.3
The Structure of a Fortran Statement
 Non-Executable Statements
 Executable Statements
 Source Code Statements
 Statement Labels
 Continuation of a Statement
2.4
(Comment Statements -> C => col 1,
! => Following characters are Comments
(See Figure 2-1, up to 132 characters)
(Fixed Form, Free Form)
(Unique to a Program Unit, 1-99999,
NOT a line number)
(& at end of non-last lines…)
The Structure of a Fortran Program
(See Figure 2-1)
(Note that Comments can be placed anywhere in a program…)
2.4.1 Declaration Section
Non Executable statements at beginning defining Program Name, Variable
Specification, i.e., PROGRAM, INTEGER
2.4.2 Execution Section
One (or more) Executable statements, i.e., WRITE, READ
2.4.3 Termination Section
STOP, END PROGRAM statements
2.4.4 Program Style
All CAPS, All lower case, Mixed, Corporate practices,
Key => Comfort but Consistency
2.4.5 Compiling, Linking and Executing the Fortran Program
Fortran Program => Fortran Compiler => Object Module => Linker => Executable
Module
2.5
Constants and Variables (Memory Locations)
Constant – a pre-defined data object
Variable – a data object that may change during program execution
Variable Name – up to 31 characters (alphabet, digits, underscore)
Key => Meaningful Names
2.5.1 Integer Constants/Variables
No decimal points, No commas, single word. (Can be 1, 2, 4 or 8 bytes)
Valid constants => 1, -1, 2147483647
Integer Variable stores an Integer value.
2.5.2 Real Constants/Variables
Decimal points, No commas, single word. (Can be 2 or 4 words)
Valid constants => 1.0, -10.0E-05, 2147483647.0, 0.31416E+01
Real Variable stores a Real value.
2.5.3 Character Constants/Variables
Character string enclosed within ‘ ‘ or “ “.
String length = 1=>32767.
Valid constants => ‘a’, ‘A1B2C3 D4E5F6’, “…..”
Character Variable stores a Character value.
2.5.4 Logical Constants/Variables
Logical Constants => .TRUE., .FALSE.
Logical Variable stores a Logical value.
2.5.5 Default and Explicit Variable Typing
 Integer, Real, Character and Logical constants are obvious by their form.
 Default Variable Types – Integer = I=>N; Real = A=>H, O=>Z
(From Original Fortran in 1954)
 Type Declaration Statements
INTEGER A,i123,BLOck
REAL q1,PGO
CHARACTER stringlength,ITISNOT
LOGICAL D5,half_true
2.5.6 Keeping Constants Consistent in a Program
 Assign Variable Name to a Constant, Use the Variable
 INTEGER, PARAMETER :: AGE=21, IQ=100
2.6
Assignment Statements and Arithmetic Calculations
 General Form: variable_name = expression
 Read i = j + k
As ‘Calculate the value of the expression, j + k, on the right side of the
equal sign, convert as necessary, and assign that value to i on the left
side of the equal sign.’
 Unary Operators => +  Arithmetic (Binary) Operators => + - * / **
 Fortran Numeric Expression Rules:
1) No two operators can occur next to each other (Use parentheses…)
2) Implied multiplication is illegal, i.e., a(b+c) => a*(b+c)
3) Use parentheses
2.6.1 Integer Arithmetic
2.6.2 Real Arithmetic
2.6.3 Hierarchy of Operations – Left to Right Evaluation, except…
 parentheses
 exponentials (multiple exponentiation => right to left…)
 multiplication/division
 addition/subtraction
2.6.4 Mixed Mode Arithmetic (A misnomer…)
 Mixed Mode Expressions
 Nres = 1.25 + 9/4
2.6.5 Mixed Mode Arithmetic and Exponentiation
 A**I => A*A*A…*A
 A**X => e**(X ln A) => involves library subprograms for e**x and ln x...
 What if A .le.0.? (OK for A**I but not for A**X…)
2.7
Assignment Statements and Logical ‘Calculations’
 General Form: Logical variable name = logical expression (T or F)
2.7.1 Relational Operators – Operate on Numeric Values, Produces Logical Result
 == .eq. ‘Equal to’
 /= .ne. ‘Not equal to’
 >
.gt. ‘Greater than’
 >= .ge. ‘Greater than or equal to’
 <
.lt. ‘Less than’
 <= .le. Less than or equal to’
 Examples: 3<4 is T; 3>4 is F; ‘A’>’B’ is F. (Alphabetical order…)
 Relational operations done after ALL arithmetic operations…
 Example: 7+3 < 2+11 : What would happen if ALL ops were left to right?
2.7.2 Logical Operators – Operate on Logical Values, Produces Logical Result
 .AND.
Logical AND;
A.AND.B => Both are True.
 .OR.
Logical OR;
A.OR.B =>
Either is True.
 .EQV.
Logical Equivalence; A.EQV.B => A and B are the same.
 .NEQV. Logical Non-Equivalence; A.NEQV.B =>A and B are different.
 .NOT.
Logical NOT;
.NOT.A =>
The reverse of A.







Hierarchy of Logical Operations: (Within parentheses…)
Arithmetic operations
Relational operations (L=>R)
.NOT. operators
.AND. operators (L=>R)
.OR. operators (L=>R)
.EQV./.NEQV. operators (L=>R)
2.8
Assignment Statements and Character Variables
 General Form: Character variable name = character expression
 A = B;
If A is longer than B, A is padded with blanks.
If A is shorter than B, extra characters in B are truncated.
2.8.1 Substring Specifications
 Character_variable_name (char1 : char2) :
Read as ‘The character string in character_variable_name from
Char1 to char2, inclusive.”
2.8.2 The Concatenation Operator (//)
 Concatenation=>Tack one character string onto the tail end of another
string.
2.8.3 Relational Operators with Character Data
 Comparison result dependent on the collating sequence on the computer.
2.9
Intrinsic Functions
 ‘Library’ or ‘Built-In’ Subprograms for common computational
problems
 z=sin(4.5*q**(b-13.5))
 Elements:
 Z, Function name, Argument(s)
 Q=REAL(3*124/12)
 Converts integer expression to REAL number
 I=INT(-16.125*COS(24/4.5*X))
 Converts real expression to INTEGER number
 I=NINT(-16.125*COS(24/4.5*X))
 Converts ROUNDED real expression to INTEGER number
2.10 List Directed Input/Output Statements
 READ (*1,*2) variable_name list
Where
*1 => input/output unit number
*2 => input/output format statement number
Variable_name list => list of variables to be input/output
2.11 Initialization of Variables
 ALWAYS INITIALIZE VARIABLES(Assignment/Read Statements,etc.)
2.12 IMPLICIT NONE Statement
 DISABLES Default Variable Typing – Forces INTEGER, REAL, etc.
 Valuable aid to compiler (and developer…)
2.13 Program Examples (Questions?)
2.14 Debugging Fortran Programs (Syntax, Logic, Run-Time Errors)
Download