| Switch Statement |
Article Index for Switch |
Website Links For Switch |
Information AboutSwitch Statement |
| CATEGORIES ABOUT SWITCH STATEMENT | |
| conditional constructs | |
|
In most languages, a switch statement is defined across many lines of code. A typical syntax is that the first line contains the actual word "switch" followed by either the name of a variable or some other expression allowed by the language's syntax. This variable or expression is usually referred to as the "control variable" of the switch statement. After this line, following lines define one or more blocks of code that represent possible branches that program execution may take. Each block begins with a line containing the case keyword followed a value that the control variable may have. If the value of the control variable matches this value, program execution will jump to that block of code. If not, the value specified in the next block (if present) is examined and the process repeats. An optional special block is also allowed, which does not specify any value and which begins with the default keyword instead of the '''case''' keyword. If this block is present and if none of the values listed for any other block matches that of the control variable, program execution will jump to the statement following the default keyword. The method of terminating a block is also of note. Typically, a break keyword is used to signal the end of the block. When encountered this keyword causes program execution to continue with the first line after the series of lines that define the switch statement, thus completing execution of the switch statement. If no break keyword is present at the end of the block, in many languages program execution "falls through" to the code associated with the next block in the switch statement, as if its value also matched the value of the control variable. One notable exception is C# , in which fallthrough is not permitted and all blocks must be terminated via a break or other keyword. EXAMPLES The following are simple examples, written in the various languages, that use switch statements to print one of several possible lines, depending on the value of an integer entered by the user. 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=5, the second case statement will produce a match to the control variable. Since there are no statements following this line and no break keyword, execution continues through the 'case 7:' line and to the next line, which produces output. The break line after this causes the switch statement to conclude. If the user types in more than one digit, the '''default''' block is executed, producing an error message. C switch(n) { case 0: printf("You typed zero. "); break; case 3: case 5: case 7: printf("n is a prime number "); break; case 2: printf("n is a prime number "); case 4: case 6: case 8: printf("n is an even number "); break; case 1: case 9: printf("n is a perfect square "); break; default: printf("Only single-digit numbers are allowed "); } Java switch (n) { case 0: System.out.println("You typed zero. "); break; case 3: case 5: case 7: System.out.println("n is a prime number "); break; case 2: System.out.println("n is a prime number "); case 4: case 6: case 8: System.out.println("n is an even number "); break; case 1: case 9: System.out.println("n is a perfect square "); break; default: System.out.println("Only single-digit numbers are allowed "); break; } Actionscript switch (variable) { case 0: trace("case number 0 tested true"); break; case 1: trace("case number 1 tested true"); break; case 2: trace("case number 2 tested true"); break;
case "A": trace("case 'A' event"); default: trace("no cases tested true"); break; }
REALbasic REALbasic uses a slightly different syntax for the same concept. It uses the traditional BASIC syntax of select case, like so: select case someInteger case 0 // Simple case of a single statement DoSomething case 1,2,5 // Using multiple case values DoSomethingElse case 8 to 10 // Using a range of case values DoSomethingYetAgain case Is > 20 // Using a comparison of case values OneMoreTime else // the catch-all (default) block NoMore end select |
|
|