The switch
statement is used to switch between multiple conditions. It is similar to using multiple series of if
statements. Using the switch
statement in that situation is very convenient and readable.
switch(value){//conditions}
It takes a value
as a parameter to match the condition defined in the switch statement. We can also pass a list of values.
Let's take a look at an example of this.
#!/usr/bin/pwsh -Commandswitch (4){1 {"Condition one."}2 {"Condition two."}3 {"Condition three."}4 {"Condition four."}Default {"No matches"}}
switch
statement and pass 4
as a value to it. switch
statement. The switch
statement checks each condition one by one. It executes the condition that matches the value that is passed to it. It executes the Default
case if no condition matches the passed value.