Values in Fortran

Fortran has various value types including strings, integers, floats, logicals, etc. Here are a few basic examples.

program values
    implicit none
    
    ! Strings, which can be concatenated with //
    print *, "fortran" // "lang"
    
    ! Integers and floats
    print *, "1+1 =", 1 + 1
    print *, "7.0/3.0 =", 7.0 / 3.0
    
    ! Logicals, with logical operators as you'd expect
    print *, .true. .and. .false.
    print *, .true. .or. .false.
    print *, .not. .true.

end program values

To run this program, save it as values.f90 and compile it using a Fortran compiler, then execute the resulting binary:

$ gfortran values.f90 -o values
$ ./values
fortranlang
 1+1 =           2
 7.0/3.0 =   2.3333333333333335     
 F
 T
 F

In this Fortran program:

  1. We use string concatenation with the // operator.
  2. Integer and floating-point arithmetic works as expected.
  3. Logical values in Fortran are denoted by .true. and .false..
  4. Logical operators in Fortran are .and., .or., and .not..

Fortran uses different syntax for printing output. The print * statement is used for standard output, similar to fmt.Println in the original example.

Note that Fortran is a statically typed language, but type declarations are not necessary in this example due to the use of implicit none. This statement turns off implicit typing, but the compiler can infer the types from the literals used.