introduction to fortran programming. faqreferencessummaryinfo reading data the if statement do loops...
TRANSCRIPT
Introduction to Fortran
Programming
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Home
Introduction
Program Structure
Getting Started
Fortran Statements
DeclarationsOpening
FilesReading
Data
The IF Statement DO Loops
OutputCompilatingRun
Program
HOME
Quiz
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Learning Objectives
The goal of this Fortran learning module is to give a quick introduction to the most common features of the Fortran 77 programming language
This includes Declarations Open external files Read input data Loops Format statements Compilating Run program
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Introduction
Fortran is a general purpose programming language, mainly intended for mathematical computations in e.g. engineering.
Fortran is an acronym for FORmula TRANslation, and was originally capitalized as FORTRAN.
Fortran was the first ever high-level programming languages. The work on Fortran started in the 1950's at IBM and there have been many versions since.
By convention, a Fortran version is denoted by the last two digits of the year the standard was proposed.
We will be using Fortran 77.Why Fortran?
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Getting started
Log on a UNIX server, in our case Petra
In the terminal window, start emacs followed by the name you wish to give your program. Fortran files are always denoted by .f
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Structure of a Program
This is the structure of a main program:
program name declarations statements stop end
In the name field, it is customary to use the same name as the Fortranfile has
A program must always end with end
Microsoft Word
Document
Here’s a Fortran Template that contains the most elementary
Arithmetic Operators in Fortran
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Fortran Statements
Fortran77 programs are typed in lines of up to 72 characters, with the first six columns of each line reserved for special purposes. As a result, Fortran77 statements ALWAYS BEGIN AT, ALWAYS BEGIN AT, OR AFTER, COLUMN 7. OR AFTER, COLUMN 7.
In emacs, the TAB key will bring you to column 7
If the firstfirst columncolumn contains ”c” or a ”*”, the the entire line would be treated as a comment.
A line can only contain 72 characters, but you will often see yourself in need of more space
A ”*” in the 6th6th columncolumn specifies that this line is a continuation of the previous
Column Position Rules
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Declarations
A Fortran program always starts with declaring which variables you will be using. A variable consists of 1-6 characters chosen from the letters a-z and the digits 0-9.
List of Fortran data types integer list of variables real list of variables character list of variables logical list of variables complex list of variables
See an exampleSee an example
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Opening External Files
Very often you need a large amount of input data and you wish to use your output data to make a chart or a graph etc
It is then very useful to have external output and input files.
Syntax used can be viewed
It is customary to open the output file together with the input file, even though it may not be written to at once
Example
Here
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Reading Data
Two ways of reading input data:1) From file2) From screen
1) Reading input data from file: First, input file must be open (see opening external files)
Then, the command is:READ(<unit number>, <format>) list of variablesUnit number is the same one as was assigned to the file when opening, format is discussed here, but it is common to use *, which means free format. After the bracket, the variables listed in the input file should be listed, in the proper order.
Read Data from Screen
Example
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Read Data from Screen
2) Read data from screenIn order to read data from screen, you must first make the program ask for input data. Such a command may be:PRINT *, ’<request for input>’
The PRINT command prints to screen, * is still free format followed by a statement
When this has been done, the program must read the inputREAD *, <variable>
The READ command allocates the input data to the pre-declaredvariable <variable>
Confused? This example should clear things up
Example
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
The IF Statement
An important part of any programming language are the conditional statements. The most common such statement in Fortran is the if statement, which actually has several forms.
The simplest one is the logical if statement: IF (logical expression) executable statement
It says that if something is true, do something
If you wish to include several statements, the general form is:
IF (logical expression) THEN statements ELSEIF (logical expression) THEN statements : :ELSE statements ENDIF
What is a logical expression?
Click to know more
See an example on the use of the if statement
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
DO Loops
The repetition of a number of statements a predetermined number of times is so important that Fortran contains a special construction that allows this to be done.
In general, a ”DO loop" may contain any Fortran statements, including another do statement, known as a "nested DO loops".
The syntax is: DO 100 INDEX=int1,int2[,int3] 'statements'
100 CONTINUE
The number 100 is a statement label (More....)INDEX is a variable, and starts at int1 in steps of int3 and ends at int2For example, DO 100 MONTHS=1,N,6 would go through all
N months in steps of six. However, Steps are normally not included as you most of the time wish to run the loop for every step in the interval
DO loop Example
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Formatted Output
When the program has calculated whatever it was supposed to calculate, it would be of no use if you dont get the results in a readable and understandable manner, and preferably to an external file so you can use your calculations in a graphical presentation etc
For this we will use the WRITE and FORMAT statements
Syntax:
WRITE(*, label) list-of-variables label FORMAT( format-code)
Explanations and Example
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
WRITE(*, label) list-of-variables label FORMAT( format-code)
Remember the statement label?
The wildcard * writes the result to screen, whereas a unit number would write to an external file assigned to this number.
A wide variety of format combinations exists.
Formatted Output
The Most Common
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Compilating
When your done writing your program, it’s time to compile your program.
What is a compilator?
We will use the following compilatorxlf -o prog fort.f
Simply write this in the UNIX terminal window. Fort.f is the name of the fortran file, if your file is called simple.f, you should write xlf –o prog simple.f
Hopefully nothing will happen, but most likely a list of errors is going to show
Errors in the Compilation Running your Program
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Run Program
When the compilation is complete it is time to run your program
Type ”prog” in your terminal window, and the program should run
If you made your program write the results to an output file, you may open the file and view the results
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
This section includes a quiz on the topics covered by this module.
The quiz is meant as a control to see if you have learned some of the most important features
Hit object to start quiz (Depending on your connection, this may take a few seconds...)
Quiz
Shockwave Flash Object
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
General information
Title: Introduction to Fortran
Teacher(s): Professor Jon Kleppe
Assistant(s): Per Jørgen Dahl Svendsen
Abstract: Provide a good background for solving problems within petroleum related topics using numerical methods
4 keywords: Fortran, loops, input/output, statments
Topic discipline: Petroleum Engineering
Level: 1
Prerequisites: None
Learning goals: Introduce the user to Fortran Programming
Size in megabytes: 0.9 MB
Software requirements: MS Power Point 2002 or later, Flash Player 6.0
Estimated time to complete:
Copyright information: The author has copyright to the module and use of the content must be in agreement with the responsible author or in agreement with http://www.learningjournals.net.
About the author
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
FAQ
For Questions, try these Computer encyclopedias:
Dataleksikon
Webopedia
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
References
Fortran tutorial
Professional Programmer's Guide to Fortran77
Introduction to Fortran Programming
FAQReferenc
esSummar
yInfo
Reading data
The IF Statement
Do Loops
Formatted output
Compilating
Run Program
Introduction
LearningObjectives
Getting started
Program Structure
Fortran Statements
Declarations
Opening files
Quiz
Summary
Subsequent to this module you should...
Have a basic understanding of how fortran is built up and how a FORTRAN program is constructed