Sign in to follow this  
Followers 0
atrX

Scripting Basics: Selection (Switch Statement)

1 post in this topic

What is a Switch Statement?

A switch statement selects a switch section to execute from a list of candidates (cases). Here's a simple example of a switch statement with 3 switch sections:

Please login or register to see this code.

In this example, 1 will be printed on the screen. Note that there is no break in the default switch section, it is not required to add a break to the default switch statement. But why is it required to have a break in the other switch sections then? Because the default switch section will ALWAYS be executed UNLESS you break the switch statement, and we don't want "Default" popping up on our screen if var is equal to 1.

 

Multiple conditions per candidate

Let's have a look at an example first:

Please login or register to see this code.

There's a couple things about this switch statement that are different compared to our first example. One of these differences is that there are multiple conditions per candidate, it's pretty straight forward how this works, I don't think any further explanation is needed.

 

But you said breaking a switch statement was required?

Yes, I did, but breaking a switch statement is only required if you have a default case, in the above example there is none, meaning we don't have to worry about that being executed.

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0