Learn

In this exercise, we will discuss the assignment and unary operators.

Assignment Operators

We can use assignment operators to assign, change, or append values to variables. The general syntax of the assignment operators is as follows: <Variable> <Assignment-Operator> <Value>.

We are already familiar with one of the assignment operators, =. It is used to assign a value to a variable. Other assignment operators include +=, -=, *=, /=, and %=. These operators are called compound assignment operators.

Notice that an arithmetic operator precedes the =. This means compound assignment operators perform operations on the values before the assignment. Consider the following example:

PS > $number = 75 PS > $number_1 = $number / 3 PS > $number_1 25 PS > $number_2 = $number PS > $number_2 /= 3 PS > $number_2 25

We can use the /= compound assignment operator as a shorthand for dividing a variable and saving the result to the same variable. As shown above, the statements $number_1 = $number / 3 and $number_2 /= 3 behave similarly.

Assignment Operators on Environment Variables

Recall that environment variables are of type String and that the + arithmetic operator can concatenate strings. We can append the += compound assignment operator to an existing environment variable.

PS > $Env:EXAMPLE_ENV_VAR = "custom value" PS > $Env:EXAMPLE_ENV_VAR += "; another value" PS > $Env:EXAMPLE_ENV_VAR custom value; another value

Note: Environment variables that have multiple values are separated by a semicolon ; on Windows. For example, the PATH environment variable specifies the directories in which executable programs are located. Utilizing ; with the += assignment operator, we can add new directories to PATH.

Unary Operators

Unary operators operate on a single variable operand. ++ and -- are the increment and decrement operators and increase or decrease the value of a variable by 1, respectively.

PS > $i = 0 PS > $i++ PS > $i 1 PS > $i-- PS > $i 0

Unary operators are an easily readable way to increase or decrease a variable value by 1.

Instructions

1.

In the script file, number_one.ps1, use the multiplication compound assignment operator to multiply the variable $number by 3.

When you’re done, click the Run button.

2.

On the next line, use the addition compound assignment operator to add 6 to the variable $number and click Run.

3.

On the next line, use the division compound assignment operator to divide the variable number by 3 and click Run.

4.

On the next line, use the subtraction compound assignment operator to subtract the variable $original_number from the variable $number.

When you’re done, click the Run button.

5.

On the next line, use a unary operator to subtract 1 from the variable $number and click Run.

6.

Finally, type ./number_one.ps1 on the terminal to run the script. Be sure to click the Run button to check your work.

When the script runs, you’ll be prompted to type a number. Enter a number into the terminal and press Enter

Was 1 your final number? Feel free to re-run the script to input a different integer. The final number will always be 1!

Take this course for free

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?