Changes

Jump to: navigation, search

Basic Windows PowerShell 1.0 Operators

1,545 bytes added, 19:52, 26 November 2008
Windows PowerShell Arithmetic Operators
<tr>
<td>%<td>Return the remainder of the division of two values</td><td>10 % 3</td>
<tr>
<td>++<td>Pre and post increment of value</td><td>$val++, ++$val</td>
<tr>
<td>--<td>Pre and post decrement of value</td><td>$val--, --$val</td>
</table>
At line:1 char:7
+ "10" / <<<< "20"=
</pre>
 
== Increment and Decrement Operators ==
 
The increment and decrement operators are inherited from the C programming language and appear in many other programming and scripting languages. For those not familiar with the concept, these operators require a little explanation. Let us start by considering the following PowerShell expressions:
 
<pre>
$x = 10
$x = $x + 1; // Increase value of variable $x by 1
 
$x = $x - 1; // Decrease value of variable y by 1
</pre>
 
These expressions increment and decrement the value of variable $x by 1. Instead of using this approach it is quicker to use the ++ and -- operators. The following examples perform exactly the same tasks as the examples above:
 
<pre>
$x++; Increment x by 1
 
$x--; Decrement x by 1
</pre>
 
These operators can be placed either before or after the variable name. If the operator is placed before the variable name, the increment or decrement is performed before any other operations are performed on the variable. For example, in the following example, $x is incremented before it is assigned to $y, leaving $y with a value of 10:
 
<pre>
$x = 9
 
$y = ++$x
 
$x
10
$y
10
</pre>
 
In the following example, the value of $x (9) is assigned to variable $y ''before'' the decrement is performed. After the expression is evaluated, therefore, the value of $y will be 9 and the value of $x will 8.
 
<pre>
$x = 9
 
$y = $x--
$x
8
$y
9
</pre>

Navigation menu