SWITCH is a Decision Making / Control Statement
This article contains 3 SWITCH code snippets on this page and another example linked here from a different page on this blog.
A Simple SWITCH Example
Output of the above code
You can play with the above code here at Playground
For programming beginners: Switch statement tests the value of a given expression (or variable) against a list of case vlues & when a match is found the statement block associated with that case is executed.
Another Example / Sample Code
Below is a sample code that finds out the country as output based on the user's selection from predefined list of cities. See the following code.
Based on the input given by the user you can see the output in the below image:
You can play around with the above code sample here.
Salient Points of SWITCH CASE
1. Default is optional.
2. A case statement can have multiple match expressions.
3. Switch cases evaluate cases from top to bottom, stopping when a case succeeds.
4. Like C# (and unlike Java, C & C++) a GO SWITCH case body breaks automatically, unless it ends with a
What is fallthrough?
In the absence of the break statement in a case block, if the control moves to the next case block without any issue, it is known as fallthrough. See the following piece of code to understand fallthrough:
Note : See line# 15 of below code for fallthrough keyword.
The output of above code is:
Fallthrough Explanation
Hope you understood fallthrough.
Question
OK...then tell me what happens if you insert a fallthrough just after case 2 i.e. at line# 11 in the above image (Captioned: fallthrough example)?
It should print the following:
two is your lucky number
three is your lucky number
So, it simply means fallthrough will affect the next case only and NOT the entire block. Feel free to play with the code here.
Please spread the word if you think this post can help a beginner :-)
Like many other popular languages, SWITCH Statement in Go is used when you have to select & execute one of many alternatives code blocks. This sounds similar to the use of long If...Else chains. Yes, but SWITCH Case comes in as a handy construct when the number of alternatives increases (say when you've got 4 or more alternatives).
- In short SWITCH is better for code readability and in some cases may be a better choice (than If...Else) for performance gains.
A Simple SWITCH Example
Switch Case Example |
You can play with the above code here at Playground
For programming beginners: Switch statement tests the value of a given expression (or variable) against a list of case vlues & when a match is found the statement block associated with that case is executed.
Another Example / Sample Code
Below is a sample code that finds out the country as output based on the user's selection from predefined list of cities. See the following code.
Based on the input given by the user you can see the output in the below image:
You can play around with the above code sample here.
- Another example of SWITCH statement Fizz-Buzz Program in Go
Salient Points of SWITCH CASE
1. Default is optional.
2. A case statement can have multiple match expressions.
3. Switch cases evaluate cases from top to bottom, stopping when a case succeeds.
4. Like C# (and unlike Java, C & C++) a GO SWITCH case body breaks automatically, unless it ends with a
fallthrough
statement. [Point#3 & 4 are related]What is fallthrough?
In the absence of the break statement in a case block, if the control moves to the next case block without any issue, it is known as fallthrough. See the following piece of code to understand fallthrough:
Note : See line# 15 of below code for fallthrough keyword.
fallthrough example |
The output of above code is:
fallthrough output code |
When you select anything between 0 to 5 (both inclusive) the code behaves as expected i.e. as the condition specified matches it prints the specific code block only and exits from the code block (i.e. SWITCH statement block).
So, where's the catch? If you enter the digit 6 (as your lucky number) the code will not exit simply after printing six is your lucky number. It will go to the next statement also and execute it and that's what is shown in above image, marked within a red rectangle.
Hope you understood fallthrough.
Question
OK...then tell me what happens if you insert a fallthrough just after case 2 i.e. at line# 11 in the above image (Captioned: fallthrough example)?
It should print the following:
two is your lucky number
three is your lucky number
So, it simply means fallthrough will affect the next case only and NOT the entire block. Feel free to play with the code here.
Please spread the word if you think this post can help a beginner :-)
No comments:
Post a Comment