Basics
Variables
Integer and Real
Modern Fortran has explicit numeric types. Integer math is exact; real math approximates with floating point.
Program
Play the program to convert an integer to real and halve it.
variables.f90
Replay: real traced execution (multi-file project)
program variables
implicit none
integer :: count
real :: half
count = 5
half = real(count) / 2.0
print '(I0, A, F0.1)', count, " ", half
end program variables
count ← 5
4real :: half5count = 56half = real(count) / 2.0values this step5counthalf ← 2.5
5count = 56half = real(count) / 2.07print '(I0, A, F0.1)', count, " ", halfvalues this step2.5half5countprint '(I0, A, F0.1)', count, " ", half
6 half = real(count) / 2.07 print '(I0, A, F0.1)', count, " ", half8end program variablesoutput5 2.5values this step5count2.5half
Follow the Values
countis declared as an integer.halfis declared as a real number.count = 5stores the whole number5.real(count) / 2.0turns5into a real and divides it to get2.5.- The program prints
5 2.5. | name | value | role | | --- | --- | --- | |count| 5 | starting integer | |real(count)| 5.0 | real version of count | |half| 2.5 | printed real result |
type declaration
`integer ::` and `real ::` give a variable its type.
type conversion
`real(count)` converts an integer to a real.
format spec
`F0.1` prints a real with one decimal and minimum width.
Exercise: variables.f90
Reproduce the output 5 2.5, then change count and predict the new half value before running it.