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 valuesTo 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
FIn this example:
- We use string concatenation with the
//operator. - Integer and floating-point arithmetic work as expected.
- Logical operations use
.and.,.or., and.not.instead of symbols. - Fortran uses
.true.and.false.for boolean values. - The
print *statement is used for output, similar tofmt.Printlnin 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.
Comments powered by Disqus