PowerShell/Expressions

This lesson introduces PowerShell expressions.

Objectives and Skills
After completing this lesson, you will be able to:
 * Describe the operators used in PowerShell scripts.
 * Explain order of operation.
 * Create PowerShell scripts to perform calculations based on user input.

Readings

 * 1)  Expression (computer science)
 * 2)  Statement (computer science)
 * 3)  Order of operations
 * 4)  Unary operation
 * 5) Microsoft TechNet: about_Operators

Multimedia

 * 1) YouTube: Windows PowerShell Fundamentals Chapter 14 - More Operators
 * 2) YouTube: PowerShell - How To - Operators

Arithmetic Operators
Arithmetic operators calculate values.

Assignment Operators
Assignment operators assign calculated values to variables.

Unary Operators
Unary operators increment or decrement a single variable by one.

Comparison Operators
Comparison operators compare values and test conditions.

Logical Operators
Logical operators compare complex conditions.

String Operators
String operators split, join, and concatenate substrings.

Activities

 * 1) Review Microsoft TechNet: about_Operators.  Experiment with different arithmetic operators to ensure you understand how they work.  Then review Microsoft TechNet: about_Operator_Precedence and MathsIsFun: Order of Operations.  Create a script that demonstrates the order of operations for PowerShell operators.
 * 2) Create a script that asks the user how old they are in years, and then calculate and display their approximate age in months, days, hours, and seconds.
 * 3) Review MathsIsFun: Conversion of Temperature.  Create a script that asks the user for a Fahrenheit temperature and then calculate and display the corresponding Celsius temperature or ask the user for a Celsius temperature and then calculate and display the corresponding Fahrenheit temperature.
 * 4) Review MathsIsFun: Area of Plane Shapes.  Create a script that asks the user for the dimensions of different shapes and then calculate and display the area of the shapes.

Lesson Summary

 * An expression is a combination of explicit values, constants, variables, operators, and functions that are interpreted according to the particular rules of precedence and of association for a particular programming language, which computes and then produces another value.
 * A statement is the smallest standalone element of an imperative programming language which expresses some action to be carried out.
 * In most languages, statements contrast with expressions in that statements do not return results and are executed solely for their side effects, while expressions always return a result and often do not have side effects at all.
 * The order of operations (sometimes called operator precedence) is a rule used to clarify which procedures should be performed first in a given mathematical expression.
 * The order of operations is exponents and roots, followed by multiplication and division, followed by addition and subtraction.
 * Parentheses are used to explicitly denote precedence by grouping parts of an expression that should be evaluated first.
 * The mnemonic PEMDAS may be used to recall the order of operations of Parentheses, Exponents, Multiplication, Division, Addition, Subtraction.
 * A unary operation is an operation with only one operand.
 * Arithmetic operators calculate values.
 * Assignment operators assign calculated values to variables.
 * Unary operators increment or decrement a single variable by one.
 * Comparison operators compare values and test conditions.
 * Logical operators compare complex conditions.
 * String operators split, join, and concatenate substrings.
 * +, -, *, /, and % are the addition subtraction, multiplication, division, and modulus (remainder) operators.
 * =, +=, -=, *=, /=, and %= are the assignment operators.
 * ++ and -- are the unary increment and decrement operators.
 * -eq, -ne, -gt, -lt, -le, and -ge are the equal, not equal, greater than, less than, less than or equal, and greater than or equal comparison operators.
 * -and, -or, -xor, and -not or ! are the and, or, exclusive or, and negation logical operators.
 * -split and -join are the string operators.

Key Terms

 * identifier
 * A name that identifies (that is, labels the identity of) either a unique object or a unique class of objects.


 * modulo (sometimes called modulus)
 * The operation that finds the remainder of division of one number by another.


 * reserved word
 * A word that cannot be used as an identifier, such as the name of a variable, function, or label.

Assessments

 * Flashcards: Quizlet: Windows PowerShell - Expressions
 * Quiz: Quizlet: Windows PowerShell - Expressions