In the [[Windows PowerShell 1.0 Flow Control with if, else and elseif]] chapter of this book we looked at how to control program execution flow using the ''if'', ''else'' and ''elseif'' statements. Whilst these statement constructs work well for testing a limited number of conditions they quickly become unwieldy when dealing with larger numbers of possible conditions. To simplify such situations, Windows PowerShell (as with most other programming and scripting langaugeslanguages) has inherited the ''switch'' statement from the C programming language. In this chapter we will explore the ''switch'' statement in detail. Those familiar with the ''switch'' statement from other programming languages might also be pleasantly surprised by a number of new ideas that the Windows PowerShell developers have added to this popular construct.
== Why Use a switch Statement? ==
In the above syntax outline ''value'' represents either a value, or an expression which returns a value. This is the value against which the ''switch'' operates. Using our example this would be the string representing the car model.
For each possible match a ''<pattern>'' is required. This can either be a single value against which the comparison is made, a wildardwildcard, regular expression or any other expression. Following on from the case lines are the Windows PowerShell statements which are to be executed in the event of the pattern match or expression evaluating to be ''true''.
The ''statements'' block may also contain an optional ''break'' or ''continue'' statement. These statements are used either to break out of the ''switch'' statement when a match is found, or skip any remaining code in a loop and begin the next iteration. If a ''break'' or ''continue'' is not specified, the switch statement will continue to work through any remaining cases even when a match has been found, potentially reducing the performance of the script.
== A switch Statement Example ==
With the above information in mind we may now construct a ''switch'' statement which provides the same functionality as our previous, and somewhat unwieldy , ''if ... elseif ... else'' construct:
<pre>
</pre>
Because there is no ''break'' after a match has been found, the switch statement moves on to other cases, resulting in the following output:
<pre>
Clearly, with a case sensitive match, only one of the switch cases will result in a match (''RedLine'').
== Using switch Statements to Iterate Through through Ranges and Collections ==
So far in this chapter we have looked at using switch statements to find matches to a single value. In fact, Windows PowerShell switch statements can be used to iterate through collections of values. A simple example involves a number range:
== Using the continue Statement ==
Exception encountered, of type "Error"
[472a0f6b] /index.php/Special:MobileDiff/7840 Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/extensions/MobileFrontend/includes/diff/InlineDiffFormatter.php(99): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): InlineDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(82): DiffFormatter->block()
#9 /var/www/techotopia/extensions/MobileFrontend/includes/diff/InlineDifferenceEngine.php(117): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): InlineDifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(241): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/extensions/MobileFrontend/includes/specials/SpecialMobileDiff.php(135): SpecialMobileDiff->showDiff()
#14 /var/www/techotopia/extensions/MobileFrontend/includes/specials/MobileSpecialPage.php(53): SpecialMobileDiff->executeWhenAvailable()
#15 /var/www/techotopia/includes/specialpage/SpecialPage.php(384): MobileSpecialPage->execute()
#16 /var/www/techotopia/includes/specialpage/SpecialPageFactory.php(553): SpecialPage->run()
#17 /var/www/techotopia/includes/MediaWiki.php(281): SpecialPageFactory::executePath()
#18 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#19 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#20 /var/www/techotopia/index.php(41): MediaWiki->run()
#21 {main}