If, else, and elseif

An if statement is written as follows; the statements enclosed by the if statement will execute if the condition evaluates to true:

if (<condition>) { 
    <statements> 
} 

The else statement is optional and will trigger if all previous conditions evaluate to false:

if (<first-condition>) { 
    <first-statements> 
} else { 
    <second-statements> 
} 

The elseif statement allows conditions to be stacked:

if (<first-condition>) { 
    <first-statements> 
} elseif (<second-condition>) { 
    <second-statements> 
} elseif (<last-condition>) { 
     <last-statements> 
}

The else statement may be added after any number of elseif statements.

Execution of a block of conditions stops as soon as a single condition evaluates to true. For example, both the first and second condition would evaluate to true as shown following, but only the first will execute:

$value = 1 
if ($value -eq 1) { 
    Write-Host 'value is 1'
} elseif ($value -lt 10) {
Write-Host 'value is less than 10' }
Implicit Boolean:
An implicit Boolean is a condition which can evaluate as true (is considered to be something) without using a comparison operator which would explicitly return true or false. For example, the number 1 will evaluate as true:
$value = 1
if ($value) {
Write-Host 'Implicit true'
}
In the previous example, the statement executes because casting the value 1 to Boolean results in true. If the variable were set to 0, the condition would evaluate to false.
Each of the following will evaluate to true as they are considered to be something when used in this manner:
[Boolean]1
[Boolean]-1
[Boolean]2016
[Boolean]"Hello world"
Each of the following will evaluate to false as each is considered to be nothing:
[Boolean]0
[Boolean]""
[Boolean]$null
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.230.81