Chapter 2:basic Elements of Fortran Programming
Chapter 2:basic Elements of Fortran Programming
Case insensitive
Example:
Apple apple ApPLe ApplE
Example:
read (*,*) Name write (*,*) name
3
Fortran Statements
Executable statements:
Actions taken by program
Examples: Read (*,*) x, y Z=x+y Write (*,*) The result = , z
Nonexecutable statements
information for proper operation of program
Examples: Program name ! This is a comment End program
Fortran Statements
Each line is 132 characters long If it does not fit, use & to split a statement
Example: Output = input1 + input2 Output = input1 & + input2 Output = input1 & & + input2
Fortran Statements
Statements can be named using a label Example: program counter 10 integer :: count = 5 20 write (*,*) count = , count end program A label should be unique It does not indicate line numbers It can be used more than once It does not indicate the program sequence/order Not used in modern Fortran 90/95
Fortran Statements
Comments: Ignored by Fortran compiler can appear any where in a line start with ! to the end of the line Examples: ! This is a counting program a = b + 1 ! This statement adds one ! Can I put a comment here? a = b + 1
7
Execution section
Actions to be performed by program
Termination section
Stopping (ending) program execution
Declaration statements
.
Executable statements
.. END Program name
Rules of NAMES
Any name (program/variable/constant) can be used only once
program counter integer :: counter = 5 write (*,*) counter = , counter end program
A$
12
Program styles
A programmer should use a consistent style:
Example 1: PROGRAM example1
REAL :: x, y, z WRITE (*,*) Enter x, y WRITE (*,*) READ (*,*) x, y, z z=x+y WRITE (*,*) x + y = , z
END PROGRAM
13
Program styles
Another programmer can use a different style:
Example 2: program example1
real :: x, y, z write (*,*) Enter x, y write (*,*) read (*,*) x, y, z z=x+y write (*,*) x + y = , z
end program
14
Program styles
This style is not acceptable (but it works!):
Example 3: program example1
real :: x, y, z WRITE (*,*) Enter x, y write (*,*) READ (*,*) x, y, z z=x+y write (*,*) x + y = , z
end PROGRAM
15
Variable:
Can change value during execution Example
REAL :: GRADE = 88 GRADE = GRADE / 100
16
Data dictionary
In the header of the program Example: program converter ! This program converts US Dollars to Omani Rials. ! We use the variables: ! USD: US Dollars ! OR: Omani Rials end program
17
1 Strings of Characters:
CHARACTER
1 Logical:
LOGICAL
Others:
Chapter 12: derived data types (not covered in this course)
18
19
20
Example2
Character (len=8) :: name name = Ramadhan Write (*,*) name
Example3
Character (len=14) :: word1 Character (len=6) :: word2 word1 = Ramadhan Word2 = kareem Write (*,*) word1, word2
21
22
Implicit none
It checks that variables types are declared Without it:
Any undeclared variable starting with I, J, K, L, M, N are integers (default typing) Other variables are real (default typing)
Examples:
Program checking read (*,*) monthly_income annual_income = monthly_income * 12 write (*,*) Annual income = , annual_income End program
23
Initializing Variables
Three ways to initialize
Initialize at declaration section Using assignment statement at execution section Using READ to initialize from input device
Non-initialized variables might or might not produce an error. Program might work in some machines and fail in others or at the same machine might work some times and fail other times depending on the values stored at the memory location.
24
Input/output statments
READ (*,*)
Standard input device (keyboard) Free input format (decided by variable type) e.g: (try inputting more values for each statement)
READ i READ i, j READ i, j, x, char (Note: character with specific length will be left justified with all others filled with blank if not entered)
WRITE(*,*)
Standard output device (screen) Free output format E.g:
WRITE(*,*) x WRITE(*,*) Result is: , x WRITE(*,*) Result is: , COS(x) WRITE(*,*) Result is: , x, And cosine will be: , cos(x)
25
Arithmetic operators
Assignment:
Variable_name = expression Example:
Days = months * 30
Rules
No two operators may occur side by side
A*-b A ** -2 A ** (-2)
27
28
29
Be careful..
30
Evaluating expressions
Example:
Distance = 0.5 * accel * time **2 Distance = (0.5 * accel * time) **2
Rules:
Parentheses first ( innermost) 2*(3+(42)2) Exponentials (right to left) 2 **2 **3 = 2 **8 = 256 Multiplication & Division (left to right) 2*4/6 Additions & Subtractions (left to right) 2 + 6 - 12
31
Evaluating expressions
Exercise: Power = (2 6) + ( 2 1 * (5+5) **2 **0) 8 = (2 6) + ( 2 1 * (10) **2 **0) 8 = (2 6) + ( 2 1 * (10) **1) 8 = (2 6) + ( 2 1 * 10) 8 = (2 6) + ( 2 10) 8 = 4 + ( 8) 8 = 488 = 20 Note: parentheses must be balanced.
32
Mixed-Mode expressions
1 + 1 /4 = 1 1. + 1 / 4 = 1. 1 + 1. / 4 = 1.25 Rule:
An integer is automatically converted into real in case of mixed arithmetic
33
Mixed-Mode expressions
1 + 1 /4 = 1 1. + 1 / 4 = 1. 1 + 1. / 4 = 1.25 Rule:
An integer is automatically converted into real in case of mixed arithmetic Raising a negative number to real power is not possible
2 ** 2 = 4 -2 ** 2 = 4
4 ** 0.5 = 2 -2 ** 0.5 ??
34
Data conversion
To convert real to integer, use INT
Anything after the decimal point is truncated Example: INT(3.3) = 3 INT(3.) = 3 INT(0.3) = 0
INTRINSIC FUNCTIONS
Functions:
Generic functions (accept more than one type of inputs) Specific functions (accept one data type only)
Examples:
SQRT(X) ABS(X) SIN(X), COS(X), TAN(X) ASIN(X), ACOS(X), ATAN(X) EXP(X) LOG(X), LOG10(X) MAX(A,B), MIN(A,B) MOD (A,B) [X in radian] [result in radian]
36
More Examples
y = SIN (theta) y = SIN(theta*(3.141593/180.) ! theta is in degrees REAL, PARAMETER :: deg_2_rad = 3.141593/180. ! a constant to convert from !degrees to radian y = SIN (theta*deg_2_rad) y =SIN(2.57) y = SIN (x) y = SIN(pi*x) y = SIN(SQRT(x))
Good practice:
Use IMPLICIT NONE Echo all inputs Initialize all variables Use parentheses properly If statement is very long break it into multiple lines Make sure all function and variables in same units
38
Example 2-3 Design a Fortran program that reads an input temperature in degrees Fahrenheit, converts it to an absolute temperature in kelvins, and writes out the result.
Example 2-4
Given the rms voltage of the power source V and the magnitude and angle of the impedance Z, write a program that calculates the rms current I, the real power P, reactive power Q, apparent power S, and power factor PF of the load.