












Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Essential tips and examples for fortran programming beginners. Topics include using the correct file extension, handling real and integer variables, and understanding order of operations and input/output statements. Learn how to avoid common mistakes and make your code more readable.
Typology: Slides
1 / 20
This page cannot be seen from the preview
Don't miss anything!













Fortran programming basics (2)
A few extra things from last week…
Example:
C = A/B! Produces C = 10./4. = 2.
C = A/B! Produces C = 10./4. = 2.
KL = K/L! Produces KL = 10/4 = 2
! Rounded down to nearest integer
D = A/L! A/L = 10.0/4 = 2.
! A/L is treated as REAL ! D is REAL and has value 2.
a. Stuff inside parentheses is done first.
b. Inside parentheses, the order is:
c. Exponentiation (A**2) – right → left
e. Addition & subtraction: right → left
Example:
X = SQRT(B2 – 4.0AC)
X = SQRT(B2 – 4.0AC)
X = SQRT(B2 – 4.0AC)
X = SQRT(B2 – 4.0AC) X = SQRT(B2 – 4.0AC)
X = SQRT(B2 – 4.0AC)
But…caution about parentheses…
Parentheses matter!!!
And…
Will not run!
Why??
Parentheses really matter!!!
Results may be printed “ugly”, such as:
We can make things a bit nicer, as in:
PRINT*, ‘value of VAR is’, VAR, ‘value of TAR is’, TAR
Would → value of VAR is 4.500000 value of TAR is 7.
We can make things better still using the WRITE command (Chapter 5)(or sooner!)
READ* statement is the simplest way to input data to a program.
In lab-02, you ran “add2.f” to add 2 numbers.
The code prompted you for two numbers.
How?
Using the READ* statement. Docsity.com
To help you see what is going on in your code, it is good practice to add some PRINT statements:
PRINT, ‘enter values for A and B’ READ, A, B
Or:
PRINT, ‘enter first number’ READ, A PRINT*, ‘enter second number’ Docsity.com
This is a style thing!
There is a more powerful READ statement – Cht. 5
“selective execution”
Fortran equivalent to:
“IF it’s Sunday, sleep in. ELSE, set alarm.”
Practice finding problems with REAL and INTEGER numbers mixed.
READ* and PRINT* statements