Switch statement: Difference between revisions

Content deleted Content added
"fallthrough" statement in go
Line 61:
The following are simple examples, written in the various languages, that use switch (or switch-like) statements to print one of several possible lines, depending on the value of an integer entered by the user.
 
===C, C++, C#, Java, php, ActionScript===
In [[C (programming language)|C]] and similarly-constructed languages, the lack of '''break''' keywords to cause fall through of program execution from one block to the next is used extensively. For example, if n=2, the fourth case statement will produce a match to the control variable. The next line outputs "n is an even number.". As an apparent bug, execution continues through the next 3 case statements and to the next line, which outputs "n is a prime number." which is a common error due to a missing '''break''' statement. The '''break''' line after a case block causes the switch statement to conclude. If the user types in more than one digit, the '''default''' block is executed, producing an error message by executing the '''default''' code.