Categories

A switch in time

I’ve posted about the switch statement a number of times but it is one of those topics that keeps coming up. Imagine you have a variable that can take one of several values. Depending on the value you might want to perform different actions like this

"red", "blue", "white", "black", "green", "yellow" |            
foreach {            
$colour = $_            
            
if ($colour -eq "red"){Write-host "Apples can be red"}            
if ($colour -eq "blue"){Write-host "Balloons can be blue"}            
if ($colour -eq "white"){Write-host "Snow can be white"}            
if ($colour -eq "black"){Write-host "Coal can be black"}            
if ($colour -eq "green"){Write-host "Grass can be green"}            
if ($colour -eq "yellow"){Write-host "Custard can be yellow"}            
}


This is a whimsical example but my excuse is that its been a long week. 



The code compares the variable to a number of values and when it finds a match something happens – in this case some text is printed.



This code works but it is overly verbose and each and every if statement is executed which is inefficient



The switch statement was designed for this situation where you need to perform multiple comparisons



"red", "blue", "white", "black", "green", "yellow" |            
foreach {            
switch ($_){            
 "red"    {Write-host "Apples can be red"}            
 "blue"   {Write-host "Balloons can be blue"}            
 "white"  {Write-host "Snow can be white"}            
 "black"  {Write-host "Coal can be black"}            
 "green"  {Write-host "Grass can be green"}            
 "yellow" {Write-host "Custard can be yellow"}            
} # end of switch            
} # end of foreach


You define the variable you will test – in this case $_



The possible values are listed and the actions to take define in the script block.



This can be improved – what if a colour isn’t defined in the switch statement.  In this case you use the default statement to catch it



"red", "blue", "white", "black", "green", "yellow", "cyan" |            
foreach {            
switch ($_){            
 "red"    {Write-host "Apples can be red"}            
 "blue"   {Write-host "Balloons can be blue"}            
 "white"  {Write-host "Snow can be white"}            
 "black"  {Write-host "Coal can be black"}            
 "green"  {Write-host "Grass can be green"}            
 "yellow" {Write-host "Custard can be yellow"}            
 default {Write-Host "can't process $($_)"}            
} # end of switch            
} # end of foreach


One final improvement is to make the switch statement terminate once its made a match.  if you did this



"red", "blue", "white", "black", "green", "yellow", "cyan" |            
foreach {            
switch ($_){            
 "red"    {Write-host "Apples can be red"}            
 "blue"   {Write-host "Balloons can be blue"}            
 "white"  {Write-host "Snow can be white"}            
 "black"  {Write-host "Coal can be black"}            
 "green"  {Write-host "Grass can be green"}            
 "blue"   {Write-host "Bikes can be blue"}            
 "yellow" {Write-host "Custard can be yellow"}            
 default {Write-Host "can't process $($_)"}            
} # end of switch            
} # end of foreach


but really you want to stop the comparison when you get a match so you use the break keyword



"red", "blue", "white", "black", "green", "yellow", "cyan" |            
foreach {            
switch ($_){            
 "red"    {Write-host "Apples can be red"; break}            
 "blue"   {Write-host "Balloons can be blue"; break}            
 "white"  {Write-host "Snow can be white"; break}            
 "black"  {Write-host "Coal can be black"; break}            
 "green"  {Write-host "Grass can be green"; break}            
 "blue"   {Write-host "Bikes can be blue"; break}            
 "yellow" {Write-host "Custard can be yellow"; break}            
 default {Write-Host "can't process $($_)"}            
} # end of switch            
} # end of foreach


In this case the second test on “blue” won’t happen



Switch can get a lot more complicated than this so I recommend you read the help file



get-help about_switch

Leave a Reply