Hello World in Fortran
Hello World in Fortran
PROGRAM HelloWorld
PRINT *,'Hello world!'
END PROGRAM HelloWorld
1
2
Goals are to be able to . . .
Introduction to FORTRAN
8
Fortran Syntax
Program is contained in a text file
called source code or source file
Source code must be processed by a compiler to create an
executable
Source file suffix can vary, but we will always use .f90
(.for, .f, .F, .f90, .F90, …)
Since source file is simply text, can be written using any
text editor
Introduction to FORTRAN
9
Fortran Syntax (cont’d)
First statement in code is program statement
Followed by program name
program myprog (first line in source code)
Suggestion: give the source file the same name as the
program
myprog.f90 (name of source file)
Last statement is a corresponding end program myprog
(myprog optional)
Language is not case sensitive ( PROGRAM myProg works)
Single blank space serves as delimiter
But white space (multiple consecutive blanks) is ignored
Types of Data
Integers
Integers are numbers without a decimal point.
e.g. -999 , +17, 1234 (not allowed: 1,234 , +17. )
No commas may be embedded within an integer
constant.
If the number is positive, the sign + is optional, but the -
is required to for negative.
Integer variables contain the value of an integer data
type.
There is a maximum and a minimum value that an
integer can take. The range is determined by how
much memory is (in terms of no. of bytes) given to a
variable of the integer type.
Chapter 2: Basic Elements of Fortran
Real Numbers
Real (or floating-point) numbers represent
values with a fraction.
Real constants can be written with or without
an exponent. e.g. 10. , -999.9, 1.0E-3 (i.e.
0.001 or .001 )
Not allowed: 1,000. , 111E3, -12.0E1.5, 1.0x
10^-3
Chapter 2: Basic Elements of Fortran
Characters
A character constant is a string of characters
enclosed in a single or double quotes.
Any characters representable on a computer
(not just the Fortran characters) are legal
in a character context (i.e. if enclosed in
quotes).
e.g. ‘this is’, ‘ ‘, ‘[^]’, “3.1345”
Chapter 2: Basic Elements of Fortran
e.g.
PROGRAM test_1
IMPLICIT NONE
INTEGER :: i
REAL :: time
i=123
time=10.0
Chapter 2: Basic Elements of Fortran
Unary Operators
Unary operators (just a single operand) , but
the negate operator is one that is most
common.
x = +2
x = -a
Here, take the value on the right, apply the
operator and assign it to the variable on
the left.