演算の優先順位
演算の優先順位によって、式の中で実行される演算の順序が 決まります。 高い優先順位の演算は低い優先順位の演算の前に実行されます。
括弧は最高の優先順位を持つので、括弧の中の演算が常に最初に実行されま す。
優先順位が同じ演算 (たとえば、A+B+C) は、左から右の順序 で評価されます。ただし、** の場合は、右から左に評価されます。
(式は左から右に評価されますが、これは、オペランドも左から右に 評価されるとは意味しないので注意してください。追加の考慮事項については、評価の順序を参照してください。)
以下のリストは、演算の優先順位を最高位から最低位まで示したものです。
- ()
- 組み込み関数、ユーザー定義の関数
- 単項 +、単項 -、NOT
- **
- *, /
- 2 項 +、2 項 -
- =, <>, >, >=, <, <=
- AND
- OR
図 1 は、優先順位がどのように働くかを示しています。
図 1. 優先順位の例
*..1....+....2....+....3....+....4....+....5....+....6....+....7...+....
* The following two operations produce different results although
* the order of operands and operators is the same. Assume that
* PRICE = 100, DISCOUNT = 10, and TAXRATE = 0.15.
* The first EVAL would result in a TAX of 98.5.
* Since multiplication has a higher precedence than subtraction,
* DISCOUNT * TAXRATE is the first operation performed. The result
* of that operation (1.5) is then subtracted from PRICE.
/FREE
TAX = PRICE - DISCOUNT * TAXRATE;
// The second EVAL would result in a TAX of 13.50.
// Since parentheses have the highest precedence the operation
// within parenthesis is performed first and the result of that
// operation (90) is then multiplied by TAXRATE.
TAX = (PRICE - DISCOUNT) * TAXRATE;
/END-FREE