Operators
| Arithmetic and ** | |
| == and /= | |
| .and. .or. .not. | |
| Concatenate with // |
Arithmetic and **
Fortran uses plus, minus, star, and slash for the four basic actions, and double star for powers.
Parentheses control the order first, then powers, then multiply and divide, then plus and minus.
program arithdemo
implicit none
real :: a
a = 2.0 ** 3 + (10.0 - 4.0) / 2.0
print *, "a =", a
end program arithdemo
Mixing integer and real values in one expression converts the result toward real, so keep the types planned and consistent.
== and /=
Comparisons return a logical answer: equal, not equal, less, and greater, with optional equality on the ends.
Modern code writes them with symbols, which read the same as in many other languages.
program cmpdemo
implicit none
integer :: n
n = 5
print *, n == 5
print *, n /= 5
print *, n >= 3
end program cmpdemo
.and. .or. .not.
Logical operators combine true and false answers. Both sides stay inside dots, with dots on each end of the word.
Use parentheses to group the logic, because .and. binds tighter than .or. in the same way that multiply binds tighter than plus.
program logicdemo
implicit none
logical :: ok
ok = (3 < 5) .and. .not. (2 == 4)
print *, ok
end program logicdemo
Concatenate with //
Double slash joins strings into longer text for messages and file names.
Add explicit blanks inside quotes where needed, because concatenation never inserts spaces on its own.
program catdemo
implicit none
character(len=30) :: msg
msg = "Hello, " // "Fortran"
print *, msg
end program catdemo
Next: Conditions
Article author: Arthur Isaev