0% found this document useful (0 votes)
42 views

Hello World in Fortran

The document provides an introduction to Fortran programming including: - A simple "Hello World" Fortran program to print "Hello world!" - Fortran was designed for mathematical translations and is compiled into machine code before execution. - Chapters discuss Fortran language fundamentals like data types, variables, constants, program structure, I/O, and compiling/executing programs. - A sample program demonstrates basic Fortran syntax like declaring integer variables, reading input, performing calculations, and printing output.

Uploaded by

hussein alsaede
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

Hello World in Fortran

The document provides an introduction to Fortran programming including: - A simple "Hello World" Fortran program to print "Hello world!" - Fortran was designed for mathematical translations and is compiled into machine code before execution. - Chapters discuss Fortran language fundamentals like data types, variables, constants, program structure, I/O, and compiling/executing programs. - A sample program demonstrates basic Fortran syntax like declaring integer variables, reading input, performing calculations, and printing output.

Uploaded by

hussein alsaede
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Hello world in Fortran

PROGRAM HelloWorld
PRINT *,'Hello world!'
END PROGRAM HelloWorld

The name Fortran is acronym for FORmula TRANsation , it was


design to do easy translation for math.

1
2
Goals are to be able to . . .

write simple Fortran programs


understand and modify existing Fortran code
manage programming projects with using
subroutine
3
Chapter 1 Intro. Fortran Language
Two fundamentally different types of high-level languages:
Interpreted language
MATLAB, Python, Java
Translation to machine-language is performed
incrementally at run time
Compiled language
Fortran, C, C++
Translation is performed once, then executable is run as
frequently as needed without further translation
4
Chapter 1 Intro. Fortran Language
(cont’d)
Compiled languages run faster.
Large-scale computing is usually done with compiled
language
Interpreted languages more convenient but slower
e.g., no need to declare variables; do things on-the-fly
MATLAB can be an order of magnitude slower than C/
fortran (code dependent)
5
Fortran History
Before Fortran, programs were written in assembly language
(very tedious to say the least)
low-level commands such as “load x from memory into
register 7” or “add values in registers 10 and 11 and write
result to register 4”
Fortran was the first widely-used high-level computer
language
1957
Developed by IBM for scientific applications
Program written on a specially formatted green sheet,
Introduction to FORTRAN
then entered as punched cards
6
Fortran History
Fortran 66 (1966)
Fortran 77 (1978)
Fortran 90 (1991)
“fairly” modern (structures, etc.)
Current “workhorse” Fortran
Fortran 95 (minor differences to Fortran 90)
Fortran 2003
Gradually being implemented by compiler companies
Object-oriented support
Interoperability with C is in the standard
7
What Language Should I Use?
Generally, use the language you know best
Interpreted languages are great for
Interactive applications
Code development and debugging
Algorithm development
For major number ,compiled languages are preferred
(Fortran, C, C++)

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

Three common types of data are stored in a


computer's memory: character, integer, real
Each type has unique characteristics and takes up
a different amount of memory.
Character Data

A typical system for representing character data may


include the following:
Letters: A through Z and a through z
Digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Miscellaneous symbols, e.g. (", ', ?, ., <, >, =, %, &)
Integer Data

Integer data ( e.g. -1, -355, 0, 1993) are represented


exactly on computers. However, only a finite
number can be stored.
Real Data

The real data type stores numbers in a format similar


to scientific notation.
For example, the speed of light in a vacuum is about
299,800,000 meters per second.  The number in
scientific notation would be 2.998 * 10^8  (m/s)
meters per second.
Chapter 2: Basic Elements of Fortran

2.1 The Fortran Character Set


The following are valid in a Fortran 90/95
program:
alpha-numeric: a-z, A-Z, 0-9, and _ (the
underscore);
arithmetic symbols: +, -, *, /, **
miscellaneous symbols: e.g.
, comma
. decimal point
< less than
etc
Chapter 2: Basic Elements of Fortran

2.2 Structure of a FORTRAN Statement


A program consists of a series of statements designed
to accomplish the goal to be accomplished.
There are two basic types of statements:
Executable statements describe the actions taken by
the program (additions, subtractions,
multiplications, divisions).
Non-executable statements provide information
necessary for proper operation of the program.
Chapter 2: Basic Elements of Fortran

Rules on Fortran statements:


Each line may be up to 132 characters long.
A statement too long to fit in a single line may be
continued on the next line by ending the current
line with an & (ampersand). e. g.
output = input1 + input2 ! sum the inputs
output = input1 & ! Also, sum the inputs
+ input2
Chapter 2: Basic Elements of Fortran

The above two statements are


equivalent.
Commenting your code is very
important.  To comment in FORTRAN,
one uses the exclamation point (!).
All comments after the ! are ignored by
the compiler
Chapter 2: Basic Elements of Fortran

One can use labels in some statements. A


label can be any number between 1 and
99999.
Statement labels are less common in modern
FORTRAN. 
Chapter 2: Basic Elements of Fortran

2.3 Structure of a FORTRAN Program


A FORTRAN program can be divided into three
sections:
Declarations - This section consists of a group of
non-executable statements at the start of the
program.
Execution - This section consists of one or more
statements describing the actions to be
performed by the program.
Termination - This section consists of a
statement (or statements) telling the
Stop means stop execution of the program and back to operation system while end
computer
means the end ofto
thestop/end
program unit,running the
therefore the stopprogram.
command before the end
command is not mandatory
Chapter 2: Basic Elements of Fortran

The program below reads two numbers as input, multiplies


them, and prints out the result
PROGRAM my_first_program
! Purpose:
! To illustrate some of the basic features of a Fortran
program.
!
! Declare the variables used in this program.
INTEGER :: i, j, k ! All variables are integers
! Get two values to store in variables i and j
PRINT*, 'Enter the numbers to multiply: '
READ *, i, j
Chapter 2: Basic Elements of Fortran

! Multiply the numbers together


k=i*j
! Write out the result.
PRINT *, 'Result = ', k
! Finish up.
STOP
END PROGRAM my_first_program
Chapter 2: Basic Elements of Fortran

Discussion of Program Above


The first statement of this program begins with the
word PROGRAM.  This is a non-executable
statement that specifies the name of the
program to the FORTRAN compiler.
The name may be up to 31 characters long and be
any combination of alphabetic characters, digits,
and the underscore.
The first character must be a letter.
The PROGRAM statement must be the first line of
the program.
Chapter 2: Basic Elements of Fortran

The Declaration Section


This section begins with a comment stating that
variable declarations are to follow.
The declaration begins with the data type (INTEGER)
followed by two colons and then the variable
name.
A comment follows the variable name.  Every
variable must be commented as to its purpose in
the program.
These statements are non-executable.
Chapter 2: Basic Elements of Fortran

The Execution Section


The first statement in this section is the PRINT
statement that tells the user to enter the input.
The second statement will read the input and assign
the values to the corresponding variables.
The third statement multiplies the two variables
and the product is assigned to a third variable.
The last executable statement prints the product to
the screen.
Chapter 2: Basic Elements of Fortran

The Termination Section


The STOP statement tells the computer to stop
running the program. 
The use of the STOP command is optional here. 
The END PROGRAM statement informs the compiler
that no more statements exist.
Chapter 2: Basic Elements of Fortran

Compiling and Executing the FORTRAN Program


Before a program can be run (executed) it must be
compiled into an executable program.
In this process the code may also be linked to
various system libraries.
Chapter 2: Basic Elements of Fortran

2.4 Constants and Variables


A constant is a data object that is defined before a
program is executed and it does/can not change
during the execution of the program.
Constants are used in developing a good/correct
programs in solving math problems (e.g.
the circle constant PI ).
Chapter 2: Basic Elements of Fortran

A variable is a data object that can change value


during the execution of a program.
Referring to the sample program above:
The data objects i,j,k are variables.
Each variable (or constant) must have a unique
name inside the program.
Chapter 2: Basic Elements of Fortran

The names may contain up to 31 characters and any


combination of alphabetic characters, digits, and
the underscore.
The first character must always be alphabetic.
∙ The name we assign a variable (or constant)
should be meaningful in terms of the purpose
that it is used to solve the problem (e.g.
∙ time, distance, grade etc).
Chapter 2: Basic Elements of Fortran

2.4.1 - 2.4.3 Types of Data


There are five intrinsic (or "built-in") types of data:
Integer
Real
Character
There are two other intrinsic types that will be
introduced later (Logical, Complex).
Chapter 2: Basic Elements of Fortran

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

Mismatching or using an odd number of


quotes are common mistakes in
programming.
e.g. not correct character: ‘this is , ‘this is”
A character variable is a variable containing a
value of character data type. e.g. c = ‘this is’
Character variables/constants with more than
one character are often referred to as
strings.
The character constant '8' is different from the
integer constant 8.
Chapter 2: Basic Elements of Fortran

2.4.4 Variables and the IMPLICIT NONE


Checking a constant (e.g.7, 3.14156, 'John'), it is
easy to determine which type it may be. 
However, for a variable, we must assign a
type to that variable.  Assigning a type
reserves the memory needed to store the
data expected (e.g.4 bytes for: 7 , 3.14156
and
2 bytes/letter for: 'John').
Chapter 2: Basic Elements of Fortran

There is a default type given to all variables


for which a type is not explicitly given.
 This is used in Fortran versions before
Fortran 90. In this case, the first letter of
the names of variables or constants
determines the type.
Prior to Fortran 90: Var/const names with
letters i,j,k,l,m,n as first letter imply
INTEGER , where as the rest of the letters
imply REAL.
Note: Fortran upper/lower case is the same.
We use lower case for var’s names e.g. i, x, c
and upper case for key-words (e.g. READ(*,*),
STOP).
Chapter 2: Basic Elements of Fortran

In Fortran 90/95, 2003 we declare the type of


The REAL, INTEGER for all var/constants e.g.
INTEGER :: height
REAL :: second
 The IMPLICIT NONE used after the
keyword PROGRAM means that we must
specify a type for each variable you
declare.
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

Character types are declared similarly, e.g.


CHARACTER(len=5) :: name
The above will declare a variable called name
that can be up to 5 characters long.  If the
(len =  ) is omitted, the length is assumed to be
1.
An alternative is e.g.
CHARACTER(20) :: last_name
Chapter 2: Basic Elements of Fortran

2.4.5 Keeping Constants, Consistent


If we need a named constant in our program e.
g. circle: PI = 3.141593
We declare a constant in FORTRAN using the
PARAMETER after the type. e.g
INTEGER, PARAMETER :: num= 80
REAL, PARAMETER :: PI = 3.141593
CHARACTER(len = 7) , PARAMETER :: ERROR=
‘error 1’
Chapter 2: Basic Elements of Fortran

2.6 Assignment Statements and Arithmetic


Calculations
An assignment statement calculates a Math-
expression on the right of the equal sign
and assigns it to the variable on the left of
the equal sign.
variable_name = expression/value
e.g. i = i+1
Chapter 2: Basic Elements of Fortran

Binary Operators (+, -, *, /, **)


We can use the arithmetic operators above to
program calculations.
x=3+4
y = a*b
z = radius ** 2
The values (expressions) are resolved and
assigned to the variables on the left.
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.

You might also like