HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to FortranVariablesOperatorsConditions (if)Loops (do)ArraysFunctions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
🔁 Condition & Loop
📋 Array
🧩 Functions & Subroutines
Fortran›Operators

Operators

Arithmetic (+ - * / **), remainder with mod() and comparison operators.

Simple enough for a 5-year-old to understand

Arithmetic and mod

Example code
1
program h
2
print *, 2 ** 10
Exponent: 1024
CodeExponent: 1024
3
print *, 7 / 2
Integer division: 3
CodeInteger division: 3
4
print *, mod(23, 4)
Remainder: 3
CodeRemainder: 3
5
end program h
Code
💡 Hint: Fortran doesn't have % for remainder, mod(a,b) is used. ** is the exponentiation operator.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of dividing 23 by 4 using mod().

🎯 Expected output: 3

Loading editor…

About this topic

Fortran's arithmetic operators are familiar, and there is a separate operator for exponentiation; this is a trace of the language design aimed at writing mathematical formulas directly.

Comparison operators are written in modern form with symbols. In older code, letter abbreviations enclosed between dots are seen; both are equivalent, but the symbolic form is preferred in new code.

Logical operators are enclosed between dots. Short-circuit evaluation is not guaranteed in Fortran: the compiler may evaluate both sides of a combined condition. This makes combining index checks with access in the same condition unsafe.

What you will learn

  • Arithmetic operators and exponentiation
  • The behavior of integer division
  • Comparison operators
  • Logical operators
  • The lack of short-circuit guarantee

Who is this for?

  • Engineers translating formulas
  • Those surprised by the division result
  • Readers of legacy code

When to use?

The truncation behavior of integer division is deliberately used in index calculations; in average calculations, it is a source of silent errors.

Since there is no short-circuit guarantee, guard checks must be written in separate conditional blocks.

Common mistakes

Wrong

program test
  implicit none
  real :: ortalama
  ortalama = 7 / 2
  print *, ortalama
end program test

Since both operands are integers, the division is performed as integer division and the result is truncated to three; assigning it to a real number variable does not fix this. You need to write at least one operand as a real number.

Correct

program test
  implicit none
  real :: ortalama
  ortalama = 7.0 / 2.0
  print *, ortalama
end program test

Wrong

program test
  implicit none
  integer :: dizi(5), i
  i = 10
  dizi = 0
  if (i <= 5 .and. dizi(i) == 0) then
    print *, 'empty'
  end if
end program test

Fortran does not guarantee short-circuit evaluation; the compiler may also evaluate the second condition and an out-of-bounds array access will occur. The guard check must be done in a separate conditional block before the inner access.

Correct

program test
  implicit none
  integer :: dizi(5), i
  i = 10
  dizi = 0
  if (i <= 5) then
    if (dizi(i) == 0) then
      print *, 'empty'
    end if
  end if
end program test

Wrong

program test
  implicit none
  integer :: a
  a = 5
  if (a != 3) then
    print *, 'different'
  end if
end program test

In Fortran, the not-equal operator is written with a slash and an equals sign; the form with an exclamation mark is not valid, and the exclamation mark is also a comment starter, so the rest of the line is counted as a comment and produces strange errors.

Correct

program test
  implicit none
  integer :: a
  a = 5
  if (a /= 3) then
    print *, 'different'
  end if
end program test

Frequently asked questions

How is the exponentiation operator written?

Two asterisks are written side by side. When an integer exponent is used, the compiler converts it to repeated multiplication and it runs fast; a real exponent is calculated via logarithms and throws an error for negative bases.

How is the remainder calculated?

There are two standard functions and they behave differently for negative values: one follows the sign of the dividend, the other the sign of the divisor. If you are doing modular arithmetic, you must be careful which one you use.

How are strings concatenated?

With the double slash operator. In fixed-length strings, since the right side is padded with spaces, it is necessary to use a trim function before concatenation; otherwise, long spaces will appear in between.

Summary

Two asterisks are used for exponentiation.

The division of two integers is an integer.

The not-equal operator is written with a slash and an equals sign.

Logical operators are enclosed between dots.

Short-circuit evaluation is not guaranteed.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code