Types, Variables, Constants, Operators

By @fortran-guy3/1/2019fortran

Names in Fortran 90

Names or identifiers must adhere to the following rules:

  • cannot be longer than 31 characters

  • must be composed of alphanumeric characters and underscores

  • first character must be a letter

  • Good thing: identifiers are case-insensitive

A variable is a data whose values can be changed, unlike constants. They have similarity though, both variables and constants have a type, which can be one of the 5 intrinsic types, or a derived type.

5 intrinsic types:

  1. Integer type:

  2. Real type: real type and double precision

  3. Complex type

  4. Logical type: two logical values: .true. and .false. (yes, there are dots defending true and false)

  5. Character type: characters and strings

Consider the following program for the illustration of the concept of variables, constants and types:

1548433263578.png
  • literal constants: a constant value without a name
  • named constants: a constant value with a name, named constants and variables must be declared at the beginning of a program in a so-called type declaration statement. Named constants must be declared with the “parameter” attribute:

Example:

real, parameter:: pi=3.1415927

Variables

Variables should be declared at the beginning of a program in a type declaration statement:

Example:

integer :: total
real::average1, average2    ! this declares 2 real values
complex:: cx
logical :: done
character(len=80):: line ! a string consisting of 80 characters

then after the declaration, this statements can be used as:

Example:

total = 6.7
average1=average2
done=.true.
line = “this is a line” ! characters are enclosed in a double quotes (“)
cx = cmplx(1.0/2.0, -3.0)  ! cx = 0.5 – 3.0i, one has to use cmplx it is an intrinsic function

Arrays

Array is a collection of variables of the same type. Arrays can be 1D (like vectors in math), 2D (matrices) up to 7D. Arrays are declared with the dimension attribute:

Examples:

real, dimension(5)::vector  ! 1-dim, real array containing 5 elements
1548433599760.png
integer, dimension(3,3)::matrix  !2-dim. Integer array
1548433615494.png

Elements of an array can be referenced by specifying their subscripts.

Example:

  • 1st element of the vector is, vector(1)
  • row 1, column 1 of a matrix is matrix(1,1)

Character Strings

  • concatenation: character(len=*), parameter :: & state=”Michigan’, name=”tech”
  • characteract(len=12):: school
  • school = state // name ! // is used to concatenate state and name producing “michigantech”
1

comments