- Microsoft Exchange Server PowerShell Essentials
- Biswanath Banerjee
- 216字
- 2021-07-16 13:04:58
Switch statements
A Switch statement is used to check multiple conditions. It is equivalent to a series of If
statements. The Switch statement lists each condition and an optional action. If a condition is true, the action is performed:
Syntax:
Switch (<test-value>) { <condition> {<action>} <condition> {<action>} }
The Switch
statement compares the value of 2
to each of the conditions listed. Once the test value matches the condition, the action is performed:
PS> switch (2) { 1 {"It is one."} 2 {"It is two."} 3 {"It is three."} 4 {"It is four."} } It is two.
In the previous example, the value is compared to each condition in the list and there is a match for the value of 2
. Let's take a look at the same example where we have added another condition that matches the value of 2
:
PS> switch (2) { 1 {"It is one."} 2 {"It is two."} 3 {"It is three."} 4 {"It is four."} 2 {"Two again."} } It is two. Two again.
Using the Break statement, you can directly switch to stop the comparison after a match and terminate the switch statement:
PS> switch (2) { 1 {"It is one."} 2 {"It is two."; Break} 3 {"It is three."} 4 {"It is four."} 2 {"Two again."} } It is two.
推薦閱讀
- Mastering OpenLayers 3
- Mastering Visual Studio 2017
- 區塊鏈:以太坊DApp開發實戰
- Web Application Development with MEAN
- 程序員修煉之道:通向務實的最高境界(第2版)
- 軟件測試技術指南
- Go并發編程實戰
- 計算機應用基礎實踐教程
- Cybersecurity Attacks:Red Team Strategies
- Raspberry Pi Robotic Projects(Third Edition)
- Clojure for Java Developers
- Python網絡爬蟲技術與應用
- Learning JavaScript Data Structures and Algorithms(Second Edition)
- 從0到1:HTML5 Canvas動畫開發
- JSP程序設計與案例實戰(慕課版)