Fortran-operators
Fortran-演算子
演算子は、特定の数学的または論理的な操作を実行するようコンパイラーに指示する記号です。 Fortranは、次の種類の演算子を提供します-
- 算術演算子
- 関係演算子
- 論理演算子
これらすべてのタイプの演算子を1つずつ見ていきましょう。
算術演算子
次の表は、Fortranでサポートされているすべての算術演算子を示しています。 変数 A が5を保持し、変数 B が3を保持すると仮定します-
リンク:/fortran/fortran_arithmetic_operators [例を表示]
Operator | Description | Example |
---|---|---|
+ | Addition Operator, adds two operands. | A + B will give 8 |
- | Subtraction Operator, subtracts second operand from the first. | A - B will give 2 |
* | Multiplication Operator, multiplies both operands. | A* B will give 15 |
/ | Division Operator, divides numerator by de-numerator. | A/B will give 1 |
* * | Exponentiation Operator, raises one operand to the power of the other. | A* * B will give 125 |
関係演算子
次の表は、Fortranでサポートされているすべての関係演算子を示しています。 変数 A が10を保持し、変数 B が20を保持すると仮定します-
リンク:/fortran/fortran_relational_operators [例を表示]
Operator | Equivalent | Description | Example |
---|---|---|---|
== | .eq. | Checks if the values of two operands are equal or not, if yes then condition becomes true. | (A == B) is not true. |
/= | .ne. | Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. | (A != B) is true. |
> | .gt. | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (A > B) is not true. |
< | .lt. | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (A < B) is true. |
>= | .ge. | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (A >= B) is not true. |
⇐ | .le. | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (A ⇐ B) is true. |
論理演算子
Fortranの論理演算子は、論理値.trueでのみ機能します。 および.false。
次の表は、Fortranでサポートされているすべての論理演算子を示しています。 変数Aが.trueを保持していると仮定します。 変数Bは.falseを保持します。 、その後-
リンク:/fortran/fortran_logical_operators [例を表示]
Operator | Description | Example |
---|---|---|
.and. | Called Logical AND operator. If both the operands are non-zero, then condition becomes true. | (A .and. B) is false. |
.or. | Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. | (A .or. B) is true. |
.not. | Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. | !(A .and. B) is true. |
.eqv. | Called Logical EQUIVALENT Operator. Used to check equivalence of two logical values. | (A .eqv. B) is false. |
.neqv. | Called Logical NON-EQUIVALENT Operator. Used to check non-equivalence of two logical values. | (A .neqv. B) is true. |
Fortranでの演算子の優先順位
演算子の優先順位は、式内の用語のグループ化を決定します。 これは、式の評価方法に影響します。 特定の演算子は、他の演算子よりも優先順位が高くなっています。たとえば、乗算演算子は加算演算子よりも優先順位が高くなります。
たとえば、x = 7 + 3 * 2;ここでは、演算子*の優先順位が+よりも高いため、xには20ではなく13が割り当てられます。したがって、最初に3 * 2で乗算され、7に加算されます。
ここでは、優先順位が最も高い演算子が表の上部に表示され、優先順位が最も低い演算子が下部に表示されます。 式内では、優先順位の高い演算子が最初に評価されます。
リンク:/fortran/fortran_operators_precedence [例を表示]
Category | Operator | Associativity |
---|---|---|
Logical NOT and negative sign | .not. (-) | Left to right |
Exponentiation | ** | Left to right |
Multiplicative | */ | Left to right |
Additive | + - | Left to right |
Relational | < ⇐ > >= | Left to right |
Equality | ==/= | Left to right |
Logical AND | .and. | Left to right |
Logical OR | .or. | Left to right |
Assignment | = | Right to left |