官术网_书友最值得收藏!

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.
主站蜘蛛池模板: 汝州市| 商河县| 水富县| 阜阳市| 来安县| 山东省| 大荔县| 普兰县| 兴宁市| 奈曼旗| 花莲市| 洪雅县| 长乐市| 体育| 盐源县| 马边| 郎溪县| 平谷区| 滨州市| 关岭| 乌海市| 泽库县| 蚌埠市| 铜川市| 方山县| 龙江县| 济南市| 砀山县| 嘉定区| 通化县| 三门峡市| 盐边县| 壤塘县| 仲巴县| 台东县| 延长县| 杭锦后旗| 双江| 长顺县| 博爱县| 陈巴尔虎旗|