Values in Co-array Fortran

Our first program will demonstrate various value types including strings, integers, floats, and booleans. Here’s the full source code:

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

end program values

To run the program, save the code in a file named values.f90 and use your Fortran compiler. For example, if you’re using gfortran:

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

In this example:

  1. We use string concatenation with the // operator.
  2. Integer and floating-point arithmetic work as expected.
  3. Logical operations use .and., .or., and .not. instead of symbols.
  4. Fortran uses .true. and .false. for boolean values.
  5. The print * statement is used for output, similar to fmt.Println in the original example.

Note that Fortran is case-insensitive, so TRUE and true would be equivalent. Also, Fortran has no built-in boolean type, instead using logical type which can be .true. or .false..

This example demonstrates basic value types and operations in Fortran, providing a foundation for more complex programs.