Introduction to Operators and Operands
Introduction to Operators and Operands
Fundamental Operators
Introduction to Operators
An operation consists of combining two or more items to get or produce a new value. Therefore, an operation uses at least one symbol and at least one value. The symbol used in an operation is called an operator. An item involved in an operation is called an operand.
Introduction to Unary Operators
A unary operator is an operator that performs its operation on only one operand.
Introduction to Binary Operators
An operator is referred to as binary if it operates on two operands. An operator is ternary if it is used on three operands.
A curly bracket is one of two operators that are used to delimit a section of code. In reality, curly brackets are used in two: the left, or starting, or opening curly bracket { and the right, or ending, or closing curly bracket }. We will encounter curly brackets when we study functions.
A parenthesis is one of two operators that are used to delimit a list of items. In reality, parentheses are used in two: the left, or starting, or opening parenthesis ( and the right, or ending, or closing parenthesis ). We will encounter parentheses in later sections.
The semi-colon is used to indicate the end of a statement. In some languages (C, C++, Java, C#, etc), that semicolon is required to end a statement. In some other languages (F#, Python, etc), that semicolon is not required (in some other languages like Visual Basic, the semicolon must not be used). PowerShell doesn't require the semicolon to end a statement. This means that if you want, when you are ending a statement, you can add a semicolon. Here are examples:
PS C:\Windows\System32> $emplNbr = 927468; PS C:\Windows\System32> $fullName = 'Jonathan Simms'; PS C:\Windows\System32> $sal = 24.73; PS C:\Windows\System32> "Employee #: $emplNbr"; Employee #: 927468 PS C:\Windows\System32> Write-Output "Employee Name: $fullName"; Employee Name: Jonathan Simms PS C:\Windows\System32> Write-Host 'Hourly Salary:' $sal; Hourly Salary: 24.73
As mentioned already, since PowerShell doesn't require the semicolon to end a statement, you can omit that semi-colon.
The coma is used to separate the items of a list or group. We will encounter comas in later sections.
When you declare a variable, a memory space is reserved for it. That memory space may be empty until you fill it with a value. To "put" a value in the memory space allocated to a variable, you can use the assignment operator represented as =. Based on this, the assignment operation gives a value to a variable. Its syntax is:
$variable-name = value
The variable-name factor must be a valid variable name. It cannot be a value such as a numeric value or a (double-quoted) string. Here is an example that assigns a numeric value to a variable:
[double]$salary
# Using the assignment operator
$salary = 12.55
Once a variable has been declared and assigned a value, you can use it.
Single-Quotes ' and Double-Quotes "
The single-quote ' and the double-quote " are heavily used in PowerShell, mostly in strings. We have seen examples and previous lessons and sections. We will see even more in other sections and lessons.
Introduction to Arithmetic Operators
A numeric value is referred to as unsigned if it displays without a sign. An unsigned numeric value is automatically positive.
A numeric value is said to be signed if it indicates whether it is positive or negative. As mentioned in the previous section, if a number doesn't display a sign, the number is said to be positive. As another way to indicate that a number is positive, you can start it with the + symbol. Notice that the + sign is optional. To indicate that a number is negative, you start it with the - symbol. This time, the - symbol is required.
The Addition Operations
Introduction
The addition is an operation used to add two values. The addition is performed using the + sign. You can add two numbers, as in a + b. You can type the operation directly on a PowerShell prompt or a Visual Studio Code terminal. The result would appear immediately. Here are examples:
PS C:\Windows\System32> 3846+9351 13197 PS C:\Windows\System32> 3884.863 + 848.5174 4733.3804
Adding Variables
To perform an addition, you can add one number to another. You can also perform the operation on variables. You can add a constant to a variable. You can type the operation directly on a PowerShell prompt or on a Visual Studio Code terminal. The result would appear immediately. Here are examples:
PS C:\Windows\System32> $sal = 24.73 PS C:\Windows\System32> $sal * 40 989.2
You can also perform the operation on two variables on a PowerShell prompt or a Visual Studio Code terminal.
If you are not planning to use the value of such an operation many times, you can perform it on Write-Host or Write-Output. The operation must be performed outside the quotes of Write-Host or Write-Output. The operation should be included inside some parentheses. Here are examples:
PS C:\Exercises> $mon = 7.5
PS C:\Exercises> $tue = 9
PS C:\Exercises> $wed = 10.5
PS C:\Exercises> $thu = 8.5
PS C:\Exercises> $fri = 8
PS C:\Exercises> $sal = 27.97
PS C:\Exercises> Write-Host 'Time Worked:' ($mon + $tue + $wed + $thu + $fri)
Time Worked: 43.5
You can also store the value in a variable, and then use that variable any way you like.
String Addition
You can apply the addition operation on strings the same way you would on numeric values. You can use the + operator on strings as in "Pie" + "Chart". This would produce "PieChart". You can also apply the operator to string variables. Here is an example:
PS C:\Windows\System32> $firstName = 'Alexander' PS C:\Windows\System32> $lastName = "Kallack" PS C:\Windows\System32> $fullName = $firstName + ' ' + $lastName PS C:\Windows\System32> Write-Output "Full Name: $fullName" Full Name: Alexander Kallack
In the same way, you can add as many variables and values as you want. Here are examples:
PS C:\Exercises> "Michael" Michael PS C:\Exercises> "Carlock" Carlock PS C:\Exercises> "Michael " + "Carlock" Michael Carlock PS C:\Exercises> "Paul" + " " + "Busbey" Paul Busbey PS C:\Exercises> "William" + " " + "Jefferson" + " " + "Clinton" William Jefferson Clinton
Addition and Other Types
You can add the value of any type to a string. The result is a new string.
Incrementing a Variable
Introduction
Incrementing a value consists of adding a unit to it. To proceed, first declare a variable and then increment it. You have two options. To start, declare a variable and initialize it. Then, to increment its value, assign the variable to itself. Yoo can then assign to it. As one technique, you can type 1 + the name of the variable, and assign that expression to the variable. Here is an example:
$number = 100 $number = 1 + $number
After performing that operation, the next time you access the variable, it would have its value incremented by 1. Consider the following examples:
PS C:\Windows\System32> $number = 100; PS C:\Windows\System32> $number = 1 + $number; PS C:\Windows\System32> Write-Output $number; 101
Pre-Incrementing a Variable
There is a special operator to perform this operation. The operator is called the increment operator and is represented by ++. Instead of writing $value = 1 + $value, you can write ++$value and you would get the same result. The above code can be written as follows:
PS C:\Windows\System32> $number = 100; PS C:\Windows\System32> ++$number; PS C:\Windows\System32> Write-Output $number; 101
++ is a unary operator because it operates on only one variable. It is used to modify the value of the variable by adding 1 to it. Every time a variable is accessed with the ++ operator, its value is incremented by 1. This is illustrated in the following code:
PS C:\Windows\System32> $number = 100 PS C:\Windows\System32> ++$number PS C:\Windows\System32> Write-Output $number 101 PS C:\Windows\System32> ++$number PS C:\Windows\System32> Write-Output $number 102 PS C:\Windows\System32> ++$number PS C:\Windows\System32> Write-Output $number 103 PS C:\Windows\System32> ++$number PS C:\Windows\System32> Write-Output $number 104 PS C:\Windows\System32> ++$number PS C:\Windows\System32> Write-Output $number 105
Becaue the ++ operator is writteon on the left side of the variable, this operation is referred to as pre-increment al.
Post-Incrementing a Variable
In the above examples, we wrote the ++ operator on the left side of the variable. In that case, the variable is incremented before it is accessed. As an alternative, you may want the variable to be incremented after it has been accessed. Once this case, as the other technique of incrementing, you can type the name of the variable + 1, and assign that expression to the variable. Here is an example:
$number = 100; $number = $number + 1; Write-Output $number; 101
The most common technique consists of writing the ++ operatore on the right side of the variable. Here are examples:
PS C:\Windows\System32> $number = 100; PS C:\Windows\System32> Write-Output ($number++); 100 PS C:\Windows\System32> Write-Output ($number++); 101 PS C:\Windows\System32> Write-Output ($number++); 102 PS C:\Windows\System32> Write-Output ($number++); 103 PS C:\Windows\System32> Write-Output ($number++); 104 PS C:\Windows\System32> Write-Output ($number++); 105
Compound Addition
To add a value to a variable and change the value that the variable is holding, you can combine the assignment "=" and the addition "+" operators to produce a new operator as +=. Here is an example:
PS C:\Windows\System32> $number = 100 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 100 PS C:\Windows\System32> $number += 15 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 115 PS C:\Windows\System32> $number += 18 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 133 PS C:\Windows\System32> $number += 24 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 157
Introduction
The subtraction operation is used to take out or subtract a value from another value. It is essentially the opposite of the addition. The subtraction is performed with the - sign. Here is an example:
PS C:\Windows\System32> [double]$value1 = 224.58
PS C:\Windows\System32> [double]$value2 = 1248.26
PS C:\Windows\System32> [double]$result = $values2 - $value1
PS C:\Windows\System32> Write-Host $value2 '-' $value1 '=' $result
1248.26 - 224.58 = -224.58
Pre-Decrementing a Variable
Decrementing a variable consists of subtracting a unit from it. As one way to perform this operation, declare a variable and assign a value to it. Then, at one time, assign the variable to itself and subtract 1 from it. This can be done as follows:
PS C:\Windows\System32> $number = 100
PS C:\Windows\System32> $number = $number - 1
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 99
A faster way to perform this operation, you can use an operator written as --. You have two options. If you:
PS C:\Windows\System32> $number = 100
PS C:\Windows\System32> --$number
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 99
In the same way, you can apply the -- operator on the left side of the variable. Here are examples:
PS C:\Windows\System32> $number = 100 PS C:\Windows\System32> --$number PS C:\Windows\System32> Write-Host 'Number:' $number Number: 99 PS C:\Windows\System32> --$number PS C:\Windows\System32> Write-Host 'Number:' $number Number: 98 PS C:\Windows\System32> --$number PS C:\Windows\System32> Write-Host 'Number:' $number Number: 97 PS C:\Windows\System32> --$number PS C:\Windows\System32> Write-Host 'Number:' $number Number: 96 PS C:\Windows\System32> --$number PS C:\Windows\System32> Write-Host 'Number:' $number Number: 95
This operation is referred to as pre-decrementing.
Post-Decrementing a Value
As another technique to decrement a variable, you can write the -- operator to its right. Here are examples:
PS C:\Windows\System32> $number = 100 PS C:\Windows\System32> $number-- PS C:\Windows\System32> Write-Host 'Number:' $number Number: 99 PS C:\Windows\System32> $number-- PS C:\Windows\System32> Write-Host 'Number:' $number Number: 98
Compound Subtraction
To decrement a value from a variable, you can use the -= operator. Here is an example:
PS C:\Windows\System32> $number = 100 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 100 PS C:\Windows\System32> $number -= 15 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 85 PS C:\Windows\System32> $number -= 18 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 67 PS C:\Windows\System32> $number -= 24 PS C:\Windows\System32> Write-Host 'Number:' $number Number: 43
Introduction
The multiplication consists of adding one value to itself a certain number of times. The multiplication is perform using the * symbol. Here is an example:
PS C:\Windows\System32> [double]$value1 = 217.973 PS C:\Windows\System32> [double]$value2 = 83.726 PS C:\Windows\System32> [double]$result = $value1 * $value2 PS C:\Windows\System32> Write-Host $value1 '*' $value2 '=' $result 217.973 * 83.726 = 18250.007398
Compound Multiplication
You can multiply a value by a variable and assign the result to the same variable. This operation is referred to as compound multiplication. Here is an example:
PS C:\Windows\System32> [double]$number = 177.368
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 177.368
PS C:\Windows\System32> $number += 26.37
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 203.738
Introduction
The division consists of fractioning a value a certain number of times. This operation is performed with the / symbol that separates two values. You can type the expression at a PowerShell command prompt or in the Visual Studio Code terminal. Here is an example:
PS C:\Windows\System32> 29731 / 83 358.204819277108
Compound Division
You can divide a variable by a certain value but assign the new value to the variable itself. This operation is referred to as compound division. Here is an example:
PS C:\Windows\System32> [double]$number = 2638.47
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 2638.47
PS C:\Windows\System32> $number = $number / 7.93
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 332.7200504413619
As faster way to perform this operation is by using an operator written as /=. Here is an example of using it:
PS C:\Windows\System32> [double]$number = 2638.47
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 2638.47
PS C:\Windows\System32> $number /= 7.93
PS C:\Windows\System32> Write-Host 'Number:' $number
Number: 332.7200504413619
Introduction
The remainder operation is performed using the percent operator (%). Here is an example:
PS C:\Windows\System32> $players = 18 PS C:\Windows\System32> $remainder = $players % 11 PS C:\Windows\System32> Write-Host 'Players:' $players Players: 18 PS C:\Windows\System32> Write-Host 'Remainder:' $remainder Remainder: 7
The Compound Remainder
The compound remainder is performed using the %= operator.
|
|||
Previous | Copyright © 2001-2025, FunctionX | Last Update: Friday 11 April 2025, 20:04 | Next |
|