Operator precedence

Mathematical operations are executed in a specific order. For example, consider the following two simple calculations:

3 + 2 * 2 
2 * 2 + 3 

The result of both of the preceding expressions is 7 (2 multiplied by 2, then add 3).

PowerShell, and most other programming languages, will calculate elements of an expression using multiplication (*), division (/), and remainder (%) first. Addition (+) and subtraction (-) are calculated next.

PowerShell has two additional operators in this category, -shl and -shr. These two have the lowest precedence and are only executed after other operations. For example, the result of the following calculation will be 128:

2 * 4 -shl 2 + 2 

First, 2 * 4 is calculated, followed by 2 + 2, and then -shl is used. The -shl operator is discussed in detail in Shift left and shift right operators.

Consider the following example:

(3 + 2) * 2 

Expressions in parentheses are always calculated first to cater for more advanced situations. For example, the result of the above calculation is 10.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.202.187