Switch, break, and continue
By default, switch
executes every case where the case evaluates as true when compared with the value.The break
and continue
keywords may be used within the switch
statement to control when testing should stop.
- When
break
is used in a case, theswitch
statement ends. - When
continue
is used, and the value is a scaler, the switch statement ends. - When
continue
is used and the value is an array, moves to the next element.
break
is often most appropriate if the value is a scalar; continue
when the value is an array. However, either may be used as needed.The switch
statement will not stop testing conditions unless the break
keyword is used. In the example below, all conditions are tested:
$value = 1
switch ($value) {
1 { Write-Host 'value is 1' }
1 { Write-Host 'value is still 1' }
}
The example above will show the output below:
value is 1
value is still 1
In the following example, where switch is acting on an array, both statements...