Google
 
   
Login
Username:

Password:


Lost Password?

Register now!
Search
Main Menu
top books
Polls
What do you think about php-deluxe.net?
Excellent!
Cool
Hmm..not bad
What the hell is this?
encyclopedia
recommendation
compare webbrowser
Freenet DSL
Who's Online
8 user(s) are online (8 user(s) are browsing encyclopedia)

Members: 0
Guests: 8

more...
browser tip
Unix Befehle
manual of unix befehle
recommendation!
Sponsored
partner

Switch statement

In computer programming, a switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., C programming language, C plus plus, C Sharp, and Java programming language). Its purpose is to allow the value of a variable or expression to control the flow of program execution.

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 Sharp, 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 programming language==

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 programming language==

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; }