1 / 15

Fortran: Language Elements

ICoCSIS. Fortran: Language Elements. Session One. Outline. Introduction: About the course Fortran – history, standards Getting started Language elements: Fortran character set: special symbols, separators, operators Tokens Source Form

sasha
Télécharger la présentation

Fortran: Language Elements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ICoCSIS Fortran: Language Elements Session One

  2. Outline • Introduction: • About the course • Fortran – history, standards • Getting started • Language elements: • Fortran character set: special symbols, separators, operators • Tokens • Source Form • Data Type (intrinsic): Integer, Real, Complex, Character, Logical • Names • Derived Data Types • Arrays • Strings • Pointers • Write your own program

  3. Introduction: About the course • The course objectives are: • To introduce students to Fortran • To develop skills for writing computational programs • Duration – six 90minutes sessions: • Language Elements • Expressions and Assignments • Control Structures • Program Units and Procedures • Array Features • Specification Statements • Approach – practical

  4. Introduction: History of Fortran • Early history: • John Backus of IBM - FORmulaTRANslation – 1950s first high level programming language • 1966 ANSI standard (known as FORTRAN IV) first standard for a programming language • FORTRAN 77 • FORTRAN 90 standard – arrays & ADT • FORTRAN 95  FORTRAN 2003  FORTRAN 2008

  5. Getting-started We will use GNU Fortran – gfortran on linux Connect to linux: webmonitor.swu.bg on port 22 Example 1: Write a program to convert temperature given in degrees Fahrenheit into degrees in Celsius and Kelvin Developed by the instructor and repeated by the students. Short explanation about I/O, Assignment and Expressions. Rewrite the program to convert temperature given in degrees Celsius into degrees Fahrenheit. Deg_F = 32.0 + (9.0/5.0)* Deg_C)

  6. Language elements: Character Set In Fortran 95, the basic .elements, or character set, are the 26 letters of the English alphabet, the 10 Arabic numerals, 0 to 9, the underscore, _, and the so-called special characters: Fortran 95 does not require the support of lower-case letters, but almost all computers nowadays support them. Within the Fortran syntax, the lower-case letters are equivalent to the corresponding upper-case letters; they are distinguished only when they form part of character sequences. The special characters $ and ? have no specific meaning.

  7. Language elements: Tokens • Alphanumeric characters (the letters, the underscore, and the numerals) may be combined into sequences that have one or more meanings. • The special characters are used to separate such sequences and also have various meanings. • Basic significant sequences of alphanumeric characters or of special characters are referred to as tokens; they are labels, keywords, names, constants (other than complex literal constants), and separators, which are / ( ) (/ /) , = => : :: ; % (in Fortran 2003 [ and ] are also separators) • Apart from within a character string or within a token, blanks may be used freely to improve the layout. • Adjacent keywords must normally be separated, but some pairs of keywords, such as else if, are not required to be separated. Adjacent keywords where separating blanks are optional. * Fortran 2003 onwards; ** Fortran 2008 only.

  8. Language elements: Source Form (1) Lines – up to 132 characters – a single statement x = (-y + root_of_discriminant)/(2.0*a) Comments follow “!” x = y/a - b ! Solve the linear equation Note: Any comments may fill the line and may contain system dependent characters not restricted by the Fortran character set. Any line whose first non-blank character is an exclamation mark, or contains only blanks, or which is empty, is purely commentary and is ignored by the compiler. Long lines - continuation mark is the ampersand (&) as the last not-comment symbol: x = & ! solve (-y + root_of_discriminant) & ! quadratic /(2.0*a) ! Equation Short statements – use “;” as statement separator a = 0; b = 0; c = 0 Labels – one to five digits, preceding the statement (leading zeros are not counted) 100 continue

  9. Language elements: Data Types A data type consists of a set of data values, a means of denoting those values, and a set of operations that are allowed on them. • Constants Vs. Variables • Intrinsic data types – default and “kinds”, defined by parameters: • Integer (−2147483648 to +2147483647 for 32 bit computer) • Real (-10.6e-11 or 3.141592653) • Complex (1., 3.2) • Character (’Anything goes’ or “a string”) ‘ and “ are delimiters • Logical (.true. and .false.) • Derived data types

  10. Language elements: Names • Name consist of between 1 and 31 alphanumeric characters12 – letters, underscores, and numerals – of which the first must be a letter. integer :: i real :: a complex :: current logical :: pravda character :: letter Or integer(kind=4) :: i real(kind=long) :: a character(len=20, kind=1) :: english_word character(len=20, kind=kanji) :: kanji_word

  11. Language elements: Derived Data Types Fortran allows us to define amore sophisticated data type than those of the intrinsic types type person character(len=10) :: name real :: age integer :: id end type person and use it further to define variables type(person) :: you & variable person( ’Smith’, 23.5, 2541) & constant Components of a composite data type can be referred by using “%” as you%id

  12. Language elements: Arrays An array consists of a rectangular set of elements, all of the same type and type parameters. real, dimension(10) :: a ( 10 elements: a(1), a(2), a(3), . . ., a(10)) real, dimension(-10:5) :: vector (16 elements: vector(-10), vector(-9), . . ., vector(5)) real, dimension(5,4) :: b subscript values - integer constants, but in may be formed of a scalar integer expression- arithmetic expression whose value is scalar and of type integer. Each subscript must be within the corresponding ranges defined in the array declaration and the number of subscripts must equal the rank.

  13. Language elements: substrings Arrays of characters character, dimension(80) :: line ! 80 one char elements better use character(len=80) :: line refer elements by substring notation: line(i:j) ! iand j are of type integer line(:i) is equivalent to line(1:i) line(i:) is equivalent to line(i:80) line(:) is equivalent to line or line(1:80) Array of strings: character(len=80), dimension(60) :: page ! 60 elements with refer by page(j)(i:k) ! 80 char length

  14. Language elements: Pointers Use a name to refer to different objects during execution of the program- object that can be made to refer to other objects is called a pointer: real, pointer :: son real, pointer, dimension(:) :: x, y real, pointer, dimension(:,:) :: a Note: In the case of an array, only the rank (number of dimensions) is declared, and the bounds (andhenceshape) are taken from that of the object to which it points. nullify (son, x, y, a) allocate (son, x(10), y(-10:10), a(n, n)) real, pointer :: son => null()

  15. Write your own program • Task: Write a program that asks the user to input three numbers, then calculate • arithmetic mean: • geometric mean • harmonic mean and print them to the screen Note: • in fortran “a to the power of b” is written as a**b • Square root is sqrt()

More Related