CS 415: Programming LanguagesThe IBM 704Standard Fortran jokeFortran I program controlPunch cardsFortran history referenceInstalling Fortran on WindowsCompiling a Fortran programHello world in FortranFortran syntaxVariable declarationInput and outputOperatorsBuilt-in functionsIf statementsCase statementLoopingLoop controlDemo programSlide 21Slide 22Fortran gotchasJohn BackusSlide 26Fortran issues…11CS 415: Programming CS 415: Programming LanguagesLanguagesFortranFortranAaron BloomfieldAaron BloomfieldFall 2005Fall 200522 The IBM 704The IBM 70433 Standard Fortran jokeStandard Fortran joke““GOD is REAL (unless declared INTEGER)." GOD is REAL (unless declared INTEGER)."55 Fortran I program controlFortran I program controlIF (arithmetic expression) N1, N2, N3IF (arithmetic expression) N1, N2, N3DO N1 variable = first_value, last_valueDO N1 variable = first_value, last_value66 Punch cardsPunch cards77 Fortran history referenceFortran history referencehttp://www.ibiblio.org/pub/languages/fortran/ch1-1.htmlhttp://www.ibiblio.org/pub/languages/fortran/ch1-1.html88 Installing Fortran on WindowsInstalling Fortran on WindowsWe’ll use Fortran 77 for this courseWe’ll use Fortran 77 for this courseThe compiler has “some” Fortran 90 featuresThe compiler has “some” Fortran 90 featuresInstall Cygwin: Install Cygwin: http://www.cygwin.com/http://www.cygwin.com/It’s a Unix-like shell for WindowsIt’s a Unix-like shell for WindowsIn particular, when you install it:In particular, when you install it:Install the gcc-g77 package in the Devel sectionInstall the gcc-g77 package in the Devel sectionWe may use Ocaml – if so, then you will need to install the ocaml We may use Ocaml – if so, then you will need to install the ocaml package (also in Devel)package (also in Devel)This can be done later, tooThis can be done later, tooInstall a good editorInstall a good editorI like Emacs: I like Emacs: http://www.gnu.org/software/emacs/emacs.htmlhttp://www.gnu.org/software/emacs/emacs.htmlQuite a learning curve, but the best editor out thereQuite a learning curve, but the best editor out thereBinaries at Binaries at http://ftp.gnu.org/pub/gnu/emacs/windows/http://ftp.gnu.org/pub/gnu/emacs/windows/ You can also install and use it as part of CygwinYou can also install and use it as part of CygwinTutorials can be found onlineTutorials can be found online99 Compiling a Fortran programCompiling a Fortran programEdit the program using your favorite editorEdit the program using your favorite editorAll program code must start on the 7All program code must start on the 7thth column! column!In Cygwin, change to that directoryIn Cygwin, change to that directoryThe c drive is at /cygdrive/c, etc.The c drive is at /cygdrive/c, etc.Enter the command:Enter the command:g77 –ff90 file.fg77 –ff90 file.fThen run the file:Then run the file:./a.exe./a.exe1010 Hello world in FortranHello world in Fortran PROGRAM HelloWorldPROGRAM HelloWorld PRINT *,'Hello world!'PRINT *,'Hello world!' END PROGRAM HelloWorldEND PROGRAM HelloWorldColumn 7Column 11111 Fortran syntaxFortran syntaxLines can only be 72 characters longLines can only be 72 characters longComments start with a !Comments start with a !First 6 columns must be spacesFirst 6 columns must be spacesUnless it’s a commentUnless it’s a commentNo semi-colons after each lineNo semi-colons after each lineA newline is a statement terminatorA newline is a statement terminator1212 Variable declarationVariable declarationThe types are real, integer, etc.The types are real, integer, etc.real :: x, y, zreal :: x, y, zinteger :: a, b, cinteger :: a, b, ccharacter :: gcharacter :: gAlways put ‘implicit none’ at the beginningAlways put ‘implicit none’ at the beginningRight after the ‘program’ lineRight after the ‘program’ linePrevents implicit variable declarationPrevents implicit variable declaration1313 Input and outputInput and outputOutput statement:Output statement:print *, "(", tri1x, ", ", tri1y, ")“print *, "(", tri1x, ", ", tri1y, ")“Input statement:Input statement:read *, tri3xread *, tri3xThere are ways to do nicely formatted outputThere are ways to do nicely formatted outputWe aren’t going over themWe aren’t going over them1414 OperatorsOperatorsBoolean operators: .and., .or., .not., etc.Boolean operators: .and., .or., .not., etc.Basically the name of the operation in periodsBasically the name of the operation in periodsBoolean values are .true. and .false.Boolean values are .true. and .false.Relational operators: <, >, <=, >=, ==, /=Relational operators: <, >, <=, >=, ==, /=1515 Built-in functionsBuilt-in functionssqrt()sqrt()log()log()sin()sin()cos()cos()exp()exp()etc.etc.1616 If statementsIf statementsForms are (exp is a Boolean expression):Forms are (exp is a Boolean expression):if (if (exp exp ) then) then ......endifendifif ( if ( exp exp ) then) then ......elseelse ......endifendifif ( exp ) thenif ( exp ) then ......else if ( exp ) thenelse if ( exp ) then ......else if ( exp ) thenelse if ( exp ) then ......endifendif1717 Case statementCase statementForm is:Form is:select case ( expr )select case ( expr )case ( value )case ( value ) ......case ( value )case ( value ) ......case ( value )case ( value ) ......case defaultcase default ......end caseend caseWhere value can be:Where value can be:A single valueA single value(300:)(300:)A range of valuesA range of values(200:400)(200:400)Case default is not Case default is not requiredrequired1818 LoopingLoopingForm is:Form is:do i = 1, 10do i = 1, 10 ......end doend dodo i = 1, 10, 2do i = 1, 10, 2 ......end doend doThe first loops from 1 to The first loops from 1 to 1010The second loops from 1 The second loops from 1 to 10, but odd numbers to 10, but odd numbers onlyonly1919 Loop controlLoop controlExitExitExits the loop, not the programExits the loop, not the programCycleCycleSimilar to next or continue in other languagesSimilar to next or continue in other languagesStarts the next iteration of the loopStarts the next iteration of the loop2020 Demo Demo programprogram! This program allows the user to input the number of degrees in an angle! 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! and then computes the cosine, sine, and
View Full Document