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 modulus (%) 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 once all other operations have completed. For example, the result of the following calculation will be 128:

2 * 4 -shl 2 + 2 

2 * 4 is calculated, followed by 2 + 2, and then -shl is used. The -shl operator is discussed in detail later in this chapter.

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

(3 + 2) * 2 
..................Content has been hidden....................

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