SlideShare a Scribd company logo
1 of 25
1
CS 415: Programming
Languages
Fortran
Aaron Bloomfield
Fall 2005
2
The IBM 704
3
Standard Fortran joke
“GOD is REAL (unless declared INTEGER)."
5
Fortran I program control
IF (arithmetic expression) N1, N2, N3
DO N1 variable = first_value, last_value
6
Punch cards
7
Fortran history reference
http://www.ibiblio.org/pub/languages/fortran/ch1-1.html
8
Installing Fortran on Windows
We’ll use Fortran 77 for this course
 The compiler has “some” Fortran 90 features
Install Cygwin: http://www.cygwin.com/
 It’s a Unix-like shell for Windows
 In particular, when you install it:
Install the gcc-g77 package in the Devel section
We may use Ocaml – if so, then you will need to install the ocaml
package (also in Devel)
 This can be done later, too
Install a good editor
 I like Emacs: http://www.gnu.org/software/emacs/emacs.html
Quite a learning curve, but the best editor out there
Binaries at http://ftp.gnu.org/pub/gnu/emacs/windows/
You can also install and use it as part of Cygwin
Tutorials can be found online
9
Compiling a Fortran program
Edit the program using your favorite editor
 All program code must start on the 7th column!
In Cygwin, change to that directory
 The c drive is at /cygdrive/c, etc.
Enter the command:
 g77 –ff90 file.f
Then run the file:
 ./a.exe
10
Hello world in Fortran
PROGRAM HelloWorld
PRINT *,'Hello world!'
END PROGRAM HelloWorld
Column 7
Column 1
11
Fortran syntax
Lines can only be 72 characters long
Comments start with a !
First 6 columns must be spaces
 Unless it’s a comment
No semi-colons after each line
 A newline is a statement terminator
12
Variable declaration
The types are real, integer, etc.
real :: x, y, z
integer :: a, b, c
character :: g
Always put ‘implicit none’ at the beginning
 Right after the ‘program’ line
 Prevents implicit variable declaration
13
Input and output
Output statement:
 print *, "(", tri1x, ", ", tri1y, ")“
Input statement:
 read *, tri3x
There are ways to do nicely formatted output
 We aren’t going over them
14
Operators
Boolean operators: .and., .or., .not., etc.
 Basically the name of the operation in periods
 Boolean values are .true. and .false.
Relational operators: <, >, <=, >=, ==, /=
15
Built-in functions
sqrt()
log()
sin()
cos()
exp()
etc.
16
If statements
Forms are (exp is a Boolean expression):
if (exp ) then
...
endif
if ( exp ) then
...
else
...
endif
if ( exp ) then
...
else if ( exp ) then
...
else if ( exp ) then
...
endif
17
Case statement
Form is:
select case ( expr )
case ( value )
...
case ( value )
...
case ( value )
...
case default
...
end case
Where value can be:
 A single value
(300:)
 A range of values
(200:400)
Case default is not
required
18
Looping
Form is:
do i = 1, 10
...
end do
do i = 1, 10, 2
...
end do
The first loops from 1 to
10
The second loops from 1
to 10, but odd numbers
only
19
Loop control
Exit
 Exits the loop, not the program
Cycle
 Similar to next or continue in other languages
 Starts the next iteration of the loop
20
Demo
program
! This program allows the user to input the number of degrees in an angle
! and then computes the cosine, sine, and tangent. It continues until the
! user inputs "n" or "N".
PROGRAM angle
IMPLICIT none
! Type variables.
REAL :: cosine, sine, tangent, degrees
REAL :: pi = 3.141592
CHARACTER :: choice
DO
! Enter and read the number of degrees in the angle.
PRINT *, "Enter the number of degrees in the angle."
READ *, degrees
! Convert number of degrees in angle to radians.
degrees = degrees*(pi/180)
! Use intrinsic functions to compute values.
cosine=cos(degrees)
sine=sin(degrees)
tangent=tan(degrees)
! Print results.
PRINT *, "cosine=", cosine, " sine=", sine, " tangent=", tangent
! Give user chance to exit program.
PRINT *
PRINT *, "Would you like to do this again?"
PRINT *,"(Press n to exit - any other key to continue.)"
READ *, choice
! Exit loop if the value in choice is N or n.
IF (choice == "N" .or. choice == "n") EXIT
END DO
STOP
END PROGRAM angle
Computes
the sin,
cos, tan,
etc.
21
Demo
program
! This program averages a series of numbers input
! from the keyboard.
PROGRAM average
IMPLICIT none
! Type variables.
REAL :: data, sum, avg
INTEGER num, i
! Prompt for and enter number of numbers to average.
PRINT *,"Enter the number of numbers to average."
READ *,num
sum = 0.0
! Loop goes from 1 to number of values to average.
DO i = 1, num
! Prompt for and enter a number.
PRINT *,"Enter a value for the number"
READ *,data
! Add number to total.
sum = sum + data
END DO
! Calculate average.
avg = sum/real(num)
! Print results.
PRINT *,"The average = ",avg
STOP
END
Computes
the
average
22
Demo
program
! This program uses a function to find the average of three numbers.
PROGRAM func_ave
! Type variables in main program (a, b, and c are local variables).
REAL :: a,b,c,average
! Prompt for and get numbers to be averaged.
PRINT *,"Enter the three numbers to be averaged."
READ *, a,b,c
! Invoke function average
PRINT *,"The three numbers to be averaged are ",a,b,c
PRINT *,"The average of the three numbers is ", average(a,b,c)
STOP
END PROGRAM func_ave
! Function average
REAL FUNCTION average(x,y,z)
! Type variables in function (x, y, and z are local varialbes).
REAL :: x,y,z
! Function name contains the average the function calculates and returns.
average = (x + y + z)/3.0
RETURN
END FUNCTION average
Computes
the
average
via a
defined
function
23
Fortran gotchas
All variables must be declared at the beginning
Remember line limit of 72 characters!
 Consider:
 The 8th variable is named ‘ei’
 There is no 9th variable declared
No continuation lines in Fortran 77
== is comparison for if's
Can’t seem to be able to change the values of
parameters in functions
integer :: first, second, third, fourth, fifth, sixth, seventh, eighth, nin
Column 72!
25
John Backus
Chemistry major at UVA
(entered 1943)
Flunked out after second
semester
Joined IBM as programmer in
1950
Developed Fortran, first
commercially successful
programming language and
compiler
26
IBM 704
Fortran
manual, 1956
27
Fortran issues…
Fortran language was described using English
 Imprecise
 Verbose, lots to read
 Ad hoc
DO 10 I=1.10
Assigns 1.10 to the variable DO10I
Early Fortrans didn’t care about spaces!
DO 10 I=1,10
Loops for I = 1 to 10
(Often incorrectly blamed for loss of Mariner-I)

More Related Content

Similar to 03-fortran.ppt

1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
thuhiendtk4
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
jyothimuppasani1
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
KrishanPalSingh39
 

Similar to 03-fortran.ppt (20)

C++ lecture 01
C++   lecture 01C++   lecture 01
C++ lecture 01
 
Functions
FunctionsFunctions
Functions
 
golang_getting_started.pptx
golang_getting_started.pptxgolang_getting_started.pptx
golang_getting_started.pptx
 
C programing Tutorial
C programing TutorialC programing Tutorial
C programing Tutorial
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
write the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdfwrite the To Dos to get the exact outputNOte A valid Fraction .pdf
write the To Dos to get the exact outputNOte A valid Fraction .pdf
 
C Programming
C ProgrammingC Programming
C Programming
 
88 c programs 15184
88 c programs 1518488 c programs 15184
88 c programs 15184
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
Labsheet_3
Labsheet_3Labsheet_3
Labsheet_3
 
Microcontroller lec 3
Microcontroller  lec 3Microcontroller  lec 3
Microcontroller lec 3
 
C Tutorials
C TutorialsC Tutorials
C Tutorials
 
Python programing
Python programingPython programing
Python programing
 
Cinfo
CinfoCinfo
Cinfo
 
What is c
What is cWhat is c
What is c
 
presentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptxpresentation_c_basics_1589366177_381682.pptx
presentation_c_basics_1589366177_381682.pptx
 
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
Oh Crap, I Forgot (Or Never Learned) C! [CodeMash 2010]
 
Presentation 2 (1).pdf
Presentation 2 (1).pdfPresentation 2 (1).pdf
Presentation 2 (1).pdf
 
keyword
keywordkeyword
keyword
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

03-fortran.ppt

  • 3. 3 Standard Fortran joke “GOD is REAL (unless declared INTEGER)."
  • 4. 5 Fortran I program control IF (arithmetic expression) N1, N2, N3 DO N1 variable = first_value, last_value
  • 7. 8 Installing Fortran on Windows We’ll use Fortran 77 for this course  The compiler has “some” Fortran 90 features Install Cygwin: http://www.cygwin.com/  It’s a Unix-like shell for Windows  In particular, when you install it: Install the gcc-g77 package in the Devel section We may use Ocaml – if so, then you will need to install the ocaml package (also in Devel)  This can be done later, too Install a good editor  I like Emacs: http://www.gnu.org/software/emacs/emacs.html Quite a learning curve, but the best editor out there Binaries at http://ftp.gnu.org/pub/gnu/emacs/windows/ You can also install and use it as part of Cygwin Tutorials can be found online
  • 8. 9 Compiling a Fortran program Edit the program using your favorite editor  All program code must start on the 7th column! In Cygwin, change to that directory  The c drive is at /cygdrive/c, etc. Enter the command:  g77 –ff90 file.f Then run the file:  ./a.exe
  • 9. 10 Hello world in Fortran PROGRAM HelloWorld PRINT *,'Hello world!' END PROGRAM HelloWorld Column 7 Column 1
  • 10. 11 Fortran syntax Lines can only be 72 characters long Comments start with a ! First 6 columns must be spaces  Unless it’s a comment No semi-colons after each line  A newline is a statement terminator
  • 11. 12 Variable declaration The types are real, integer, etc. real :: x, y, z integer :: a, b, c character :: g Always put ‘implicit none’ at the beginning  Right after the ‘program’ line  Prevents implicit variable declaration
  • 12. 13 Input and output Output statement:  print *, "(", tri1x, ", ", tri1y, ")“ Input statement:  read *, tri3x There are ways to do nicely formatted output  We aren’t going over them
  • 13. 14 Operators Boolean operators: .and., .or., .not., etc.  Basically the name of the operation in periods  Boolean values are .true. and .false. Relational operators: <, >, <=, >=, ==, /=
  • 15. 16 If statements Forms are (exp is a Boolean expression): if (exp ) then ... endif if ( exp ) then ... else ... endif if ( exp ) then ... else if ( exp ) then ... else if ( exp ) then ... endif
  • 16. 17 Case statement Form is: select case ( expr ) case ( value ) ... case ( value ) ... case ( value ) ... case default ... end case Where value can be:  A single value (300:)  A range of values (200:400) Case default is not required
  • 17. 18 Looping Form is: do i = 1, 10 ... end do do i = 1, 10, 2 ... end do The first loops from 1 to 10 The second loops from 1 to 10, but odd numbers only
  • 18. 19 Loop control Exit  Exits the loop, not the program Cycle  Similar to next or continue in other languages  Starts the next iteration of the loop
  • 19. 20 Demo program ! This program allows the user to input the number of degrees in an angle ! and then computes the cosine, sine, and tangent. It continues until the ! user inputs "n" or "N". PROGRAM angle IMPLICIT none ! Type variables. REAL :: cosine, sine, tangent, degrees REAL :: pi = 3.141592 CHARACTER :: choice DO ! Enter and read the number of degrees in the angle. PRINT *, "Enter the number of degrees in the angle." READ *, degrees ! Convert number of degrees in angle to radians. degrees = degrees*(pi/180) ! Use intrinsic functions to compute values. cosine=cos(degrees) sine=sin(degrees) tangent=tan(degrees) ! Print results. PRINT *, "cosine=", cosine, " sine=", sine, " tangent=", tangent ! Give user chance to exit program. PRINT * PRINT *, "Would you like to do this again?" PRINT *,"(Press n to exit - any other key to continue.)" READ *, choice ! Exit loop if the value in choice is N or n. IF (choice == "N" .or. choice == "n") EXIT END DO STOP END PROGRAM angle Computes the sin, cos, tan, etc.
  • 20. 21 Demo program ! This program averages a series of numbers input ! from the keyboard. PROGRAM average IMPLICIT none ! Type variables. REAL :: data, sum, avg INTEGER num, i ! Prompt for and enter number of numbers to average. PRINT *,"Enter the number of numbers to average." READ *,num sum = 0.0 ! Loop goes from 1 to number of values to average. DO i = 1, num ! Prompt for and enter a number. PRINT *,"Enter a value for the number" READ *,data ! Add number to total. sum = sum + data END DO ! Calculate average. avg = sum/real(num) ! Print results. PRINT *,"The average = ",avg STOP END Computes the average
  • 21. 22 Demo program ! This program uses a function to find the average of three numbers. PROGRAM func_ave ! Type variables in main program (a, b, and c are local variables). REAL :: a,b,c,average ! Prompt for and get numbers to be averaged. PRINT *,"Enter the three numbers to be averaged." READ *, a,b,c ! Invoke function average PRINT *,"The three numbers to be averaged are ",a,b,c PRINT *,"The average of the three numbers is ", average(a,b,c) STOP END PROGRAM func_ave ! Function average REAL FUNCTION average(x,y,z) ! Type variables in function (x, y, and z are local varialbes). REAL :: x,y,z ! Function name contains the average the function calculates and returns. average = (x + y + z)/3.0 RETURN END FUNCTION average Computes the average via a defined function
  • 22. 23 Fortran gotchas All variables must be declared at the beginning Remember line limit of 72 characters!  Consider:  The 8th variable is named ‘ei’  There is no 9th variable declared No continuation lines in Fortran 77 == is comparison for if's Can’t seem to be able to change the values of parameters in functions integer :: first, second, third, fourth, fifth, sixth, seventh, eighth, nin Column 72!
  • 23. 25 John Backus Chemistry major at UVA (entered 1943) Flunked out after second semester Joined IBM as programmer in 1950 Developed Fortran, first commercially successful programming language and compiler
  • 25. 27 Fortran issues… Fortran language was described using English  Imprecise  Verbose, lots to read  Ad hoc DO 10 I=1.10 Assigns 1.10 to the variable DO10I Early Fortrans didn’t care about spaces! DO 10 I=1,10 Loops for I = 1 to 10 (Often incorrectly blamed for loss of Mariner-I)