O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Operator Precedence

If you include more than one operator in a single line of code, you need to know the order in which VB.NET will evaluate them. Otherwise, the results may be completely different from what you intended. The rules that define the order in which a language handles operators is known as the order of precedence. If the order of precedence results in operations being evaluated in an order other than the intended one, you can explicitly override the order of precedence through the use of parentheses. Indeed, we strongly recommend the use of sufficient parentheses to avoid any possible misinterpretation. Put another way, we recommend using enough parentheses so that operator precedence is no longer relevant!

When a single line of code includes operators from more than one category, they are evaluated in the following order:

Arithmetic operators
Concatenation operators
Comparison operators
Logical operators

Within each category of operators, except for the single concatenation operator, there is also an order of precedence. If multiple comparison operators appear in a single line of code, they are simply evaluated from left to right. The order of precedence of arithmetic operators is as follows:

Exponentiation (^)
Division and multiplication (/,*) (no order of precedence between the two)
Integer division (\)
Modulo arithmetic (Mod)
Addition and subtraction (+,-) (no order of precedence between the two)

If the same arithmetic operator is used multiple times in a single line of ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required