- 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.
推薦閱讀
- C及C++程序設(shè)計(第4版)
- 密碼學(xué)原理與Java實現(xiàn)
- Oracle Database In-Memory(架構(gòu)與實踐)
- 動手玩轉(zhuǎn)Scratch3.0編程:人工智能科創(chuàng)教育指南
- D3.js 4.x Data Visualization(Third Edition)
- Mathematica Data Analysis
- Spring Boot Cookbook
- 零基礎(chǔ)趣學(xué)C語言
- 響應(yīng)式Web設(shè)計:HTML5和CSS3實戰(zhàn)(第2版)
- 創(chuàng)意UI:Photoshop玩轉(zhuǎn)APP設(shè)計
- Python青少年趣味編程
- 零基礎(chǔ)學(xué)Scratch 3.0編程
- C編程技巧:117個問題解決方案示例
- 愛上C語言:C KISS
- Flink技術(shù)內(nèi)幕:架構(gòu)設(shè)計與實現(xiàn)原理