Multiplication, division, and modulus operators

The multiplication operator is able to perform simple numeric operations. For example, the result of the following expression is 5:

2.5 * 2 

The multiplication operator may also be used to duplicate strings:

'hello' * 3 

As with the addition operator, the multiplication operator will throw an error if a number is on the left of the expression:

PS> 3 * 'hello'
Cannot convert value "hello" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:2
+ 3 * 'hello'
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger

The multiplication operator may also be used to duplicate arrays. Each of the following examples creates an array containing one, two, one, and two:

@('one', 'two') * 2 
('one', 'two') * 2 
'one', 'two' * 2 

The division operator performs numeric division:

20 / 5 

An error will be thrown if an attempt to divide by 0 is made:

PS> 1 / 0
Attempted to divide by zero.
At line:1 char:1
+ 1 / 0
+ ~~~~~
+ CategoryInfo : NotSpecified: (:) [], RuntimeException
+ FullyQualifiedErrorId : RuntimeException

The modulus operator returns the remainder of whole-number (integer) division. For example, the result of the following operation is 1:

3 % 2 
Modulus can be used for alternation:
Aside from its value to math, the modulus operator may be used to alternate; to perform an action on every second, third, fourth, and so on, iteration of a loop:
1..100 | ForEach-Object {# Show the value of $_ at intervals of 5if ($_ % 5 -eq 0) {Write-Host $_}}
..................Content has been hidden....................

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