下表列出了所有Fortran語言支援的算術運算子。假設變數A=5和變數B=3則:
運算子 | 描述 | 例子 |
---|---|---|
+ | 相加運算子,相加兩個運算元。 | A + B = 8 |
- | 減法運算,第一運算元減去第二運算元。 | A - B = 2 |
* | 乘法運算子,乘兩個運算元。 | A * B = 15 |
/ | 除法運算子,通過分子除以分母。 | A / B = 1 |
** | 乘方運算,一個運算元的B次冪。 | A ** B = 125 |
例子
試試下面的例子就明白了所有可用的算術運算子在Fortran語言:
program arithmeticOp ! this program performs arithmetic calculation implicit none ! variable declaration integer :: a, b, c ! assigning values a = 5 b = 3 ! Exponentiation c = a ** b ! output print *, "c = ", c ! Multiplication c = a * b ! output print *, "c = ", c ! Division c = a / b ! output print *, "c = ", c ! Addition c = a + b ! output print *, "c = ", c ! Subtraction c = a - b ! output print *, "c = ", c end program arithmeticOp
當編譯並執行上述程式,它會產生以下結果:
c = 125 c = 15 c = 1 c = 8 c = 2