# PowerShell/Conditions

This lesson introduces PowerShell conditions, switch statements, and regular expressions.

## Objectives and Skills

After completing this lesson, you will be able to:
* Describe basic conditional statement concepts.
* Create PowerShell scripts that use if statements.
* Create PowerShell scripts that use switch statements.

## Readings

* Wikipedia: Conditional (computer programming)
* Wikipedia: Switch statement
* Wikipedia: Regular expression
* BonusBits: Mastering PowerShell Chapter 7 - Conditions

## Multimedia

* YouTube: Powershell: Learn How to Use If/Then/Else
* YouTube: Switch
* YouTube: PowerShell - How To - If Statement

## Examples
#### If

The if statement runs code blocks based on the results of one or more conditional tests.

#### Switch

The switch statement checks multiple conditions for a given value and runs the corresponding code blocks.

#### -Match

The -match operator is used to match regular expressions. For example:

#### Switch -Regex

The switch -Regex statement checks multiple conditions for a given value based on regular expression matching and runs the corresponding code blocks.

#### Break

The break statement immediately exits the nearest enclosing Foreach, For, While, Do, or Switch statement.

## Activities

* Review Microsoft Developer Network: Boolean Values and Operators. Create a script that uses different values and conditions to confirm that in PowerShell zero is false and anything non-zero is true. Also show that the explicit value of $false is 0 and the explicit value of $true is 1.
* Create a script that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use an if/elseif/else statement to display their approximate age in the selected timeframe.
* Review MathsIsFun: Conversion of Temperature. Create a script that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Use an if/elseif/else statement to determine their selection and then gather the appropriate input and calculate and display the converted temperature.
* Review MathsIsFun: Area of Plane Shapes. Create a script that asks the user what shape they would like to calculate the area for. Use an if/elseif/else statement to determine their selection and then gather the appropriate input and calculate and display the area of the shape.
* Create a script that asks the user how old they are in years. Then ask the user if they would like to know how old they are in months, days, hours, or seconds. Use a switch statement to display their approximate age in the selected timeframe.
* Review MathsIsFun: Conversion of Temperature. Create a script that asks the user if they would like to convert Fahrenheit to Celsius or Celsius to Fahrenheit. Use a switch statement to determine their selection and then gather the appropriate input and calculate and display the converted temperature.
* Review MathsIsFun: Area of Plane Shapes. Create a script that asks the user what shape they would like to calculate the area for. Use a switch statement to determine their selection and then gather the appropriate input and calculate and display the area of the shape.
* Review Regular-Expressions: How to Find or Validate an Email Address. Create a script that uses the -match operator and a regular expression to test user input email addresses.

## Lesson Summary

* Conditional statements are features of a programming language which perform different computations or actions depending on whether a programmer-specified boolean condition evaluates to true or false.
* The pseudocode structure of a conditional statement is:
* If the condition is true, the statements following the THEN are executed. Otherwise, the execution continues in the following branch – either in the ELSE block (which is usually optional), or if there is no ELSE branch, then after the END IF.
* By using ELSE IF/ELSEIF, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped.
* The PowerShell syntax for a conditional statement is:
* In PowerShell conditions, the elseif and else statements are optional.
* By default, all comparison operators (-eq, -lt, -gt, etc.) are case-insensitive. To make a comparison operator case-sensitive, precede the operator name with a "c" (-ceq, -clt, -cgt, etc.).
* In PowerShell, zero is false and anything non-zero is true. When evaluated explicitly, $false is 0 and $true is 1.
* A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via a multiway branch.
* Switch statements come in two main variants: a structured switch which takes exactly one branch, and an unstructured switch which functions as a type of GOTO.
* PowerShell switch statements function as a hybrid between structured and unstructured, where every condition in the switch is evaluated, even after a match, but only the code block immediately following any matching condition is executed.
* The PowerShell syntax for a switch statement is:
* In PowerShell switch statements, the default case is optional.
* The break statement immediately exits the nearest enclosing Foreach, For, While, Do, or Switch statement.
* A regular expression (abbreviated regex) is a sequence of characters that forms a search pattern, mainly for use in pattern matching with strings.
* Each character in a regular expression is either understood to be a metacharacter with its special meaning, or a regular character with its literal meaning.
* In regex, | indicates either|or.
* In regex, ? indicates there is zero or one of the preceding element.
* In regex, * indicates there is zero or more of the preceding element.
* In regex, + indicates there is one or more of the preceding element.
* In regex, is used to group elements.
* In regex, . matches any single character.
* In regex, matches any single character contained within the brackets.
* In regex, [^] matches any single character not contained within the brackets.
* In regex, ^ matches the start of the string.
* In regex, $ matches the end of the string.
* In regex, \w matches a word.
* In regex, \d matches a digit.
* In regex, \s matches whitespace.

## Key Terms
### Spaghetti code

    a pejorative term for source code that has a complex and tangled control structure, especially one using many GOTO statements, exceptions, threads, or other unstructured branching constructs.

### Structured programming

    A programming paradigm aimed at improving the clarity, quality, and development time of a computer program by making extensive use of subroutines, block structures and for and while loops.

### Whitespace

    Any character or series of characters that represent horizontal or vertical space.

## Assessments

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