Lecture 2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

TME 214

Elementary Computer Programming

Dr. D.A. Fadare & Dr. A.S. Adebayo


Mechanical Engineering Department
University of Ibadan 1
Running your first FORTRAN 90 Program

Type in the following exactly as shown:


!My first program
program first
print *,'This is my first program'
end program first
Run program by clicking the following from the “Build” menu in the
following order
Compile
Build
Execute

2
Program to add two numbers

program sum !a: name of program


!an example of program structure !b: a comment
real :: answer,x,y !c: declarations
print *, 'Enter two numbers' !d: output
read *, x !e: input
read *, y !e: input
answer=x+y !f :arithmetic
print *, 'The total is ', answer !g: output
end program sum !h: end of program

3
Things to Note about the programs

There are a number of general points here:


▪The program is made up of a number of lines.
▪Each line is called a statement.
▪Each statement is made up of
• variable names e.g. answer, x, y
• operators e.g. +,- etc
• keywords e.g. read, print
▪The statements are executed sequentially.

4
Analyse the program

Let's break the program down, line by line:


)The name of the program.
Keep it reasonably short and meaningful.
b) A comment explaining the purpose of the program.
Comments are indicated by an exclamation mark.
All text to the right of an exclamation mark is ignored by the
compiler.
Programmers use comments to help them remember how a
program works.
Use of appropriate comments in programs aids
understanding and is good practice. You will get extra marks for
using comments!
Program Analysis contd

c) Variables - answer, x and y are used to store floating point


numbers – we indicate this by declaring them as real.
d) print *, outputs to the screen – the asterisk means use the
default number of decimal places when the number is written
to the screen.
e) We read information from the keyboard and store the
values in x and y.
f) Do some arithmetic and store the answer in answer.
g) Output the result to the screen
h) Conclude the program
More on Input and Output

Type in the following program:


program io
real :: x,y,z
print *, 'enter the values x,y and z'
read *, x,y,z
print *, 'the values you typed are for z,y,x are: ',z,y,x
end program io
Run the program
􀂉 You can enter the numbers one at a time and press the Enter key each time.
􀂉 Run the program again
􀂉 This time type all three numbers on one line separated by commas.
Look at the print statement

You might also like