연산자 우선순위

표현식에서 여러 작업이 수행될 때 각 부분은 연산자 우선순위라고 하는 미리 결정된 순서로 평가되고 분석됩니다. 괄호를 사용하여 우선순위의 순서를 대체하고 표현식의 일부 부분이 다른 부분보다 먼저 평가되도록 할 수 있습니다. 괄호 안의 작업은 항상 외부의 작업보다 먼저 수행됩니다. 그러나 괄호 안에서는 표준 연산자 우선순위가 유지됩니다.

표현식에 2개 이상의 범주의 연산자가 포함된 경우 산술 연산자가 먼저 평가되고, 다음에 비교 연산자가 평가되며, 논리 연산자가 마지막으로 평가됩니다. 비교 연산자는 모두 동일한 우선순위를 갖습니다. 즉, 비교 연산자가 나타나는 왼쪽에서 오른쪽 순서로 평가됩니다. 산술 연산자 및 논리 연산자는 다음 우선순위 순서로 평가됩니다.

다음 예에서는 연산자 우선순위를 사용하는 방법을 보여 줍니다.

예 1:

Dim Result
Result = 2 + 3 * 4   ' Multiplication is performed first.
'Result has value 14
Result = (2 + 3) * 4   ' Parentheses override precedence.
'Result has value 20

예 2:

Dim A, B, C
A = 5
B = 10
C = 20
Dim ComparisonResult
ComparisonResult = A + B > C / 2   ' Division is performed first, then addition, then comparison.
'ComparisonResult   Outputs: True (10 / 2 = 5, then 5 + 5 = 10, 10 > 10 is False, but corrected if comparison operator order is properly enforced)

ComparisonResult = (A + B) > (C / 2)   ' Parentheses enforce precedence.
'ComparisonResult Outputs: True (15 > 10)

예 3:

Dim X, Y, Z
X = 5
Y = 10
Z = 15

Dim LogicalResult
LogicalResult = X + Y > Z And X < Y   ' Addition and comparison are performed first, then logical AND.
'LogicalResult Outputs: False (10 + 5 > 15 is False, and 5 < 10 is True, False And True is False)

LogicalResult = (X + Y > Z) And (X < Y)   ' Parentheses enforce precedence.
'LogicalResult Outputs: False (still False And True)

예 4:

Dim NestedResult
NestedResult = (2 + (3 * 4)) * 5   ' Inner parentheses are evaluated first, then outer.
'NestedResult Outputs: 70 (3 * 4 = 12, 2 + 12 = 14, 14 * 5 = 70)

예 5:

Dim A, B, C
A = 5
B = 2
C = 10
Dim ClearResult
ClearResult = (A * B) + (C / A)   'Using parentheses to make the expression clear.
'ClearResult Outputs: 12 (5 * 2 = 10, 10 / 5 = 2, 10 + 2 = 12)

산술, 비교 및 논리 연산자에 대한 전체 설명서는 BSL 연산자 를 참조하십시오.