Main Menu

HOME

.Net
ASP
Assembly
C
C++
Delphi
HTML
Java
JavaScript Site
MySQL
PC interface
Powershell
Perl
PHP
VBScript
Visual Basic
XML

US Job listings




   Misc

   Amazon

   Links

The Switch-Case statement


 

The Switch-Case statement

Say you write a program where the user inputs a number 1-5 (corresponding to student grades, A(represented as 1)-D(4) and F(5)), stores it in a variable grade and the program responds by printing to the screen the associated letter grade. If you implemented this using If-Else, your code would look something like this:

if(grade == 1)
{
   printf("A\n");
}
else if(grade == 2)
{
   printf("B\n");
}
else if /* etc. etc. */
                                          

Having a long chain of if-else-if-else-if-else can be a pain, both for the programmer and anyone reading the code. Fortunately, there's a solution: the Switch-Case construct, of which the basic syntax is:

switch(/* integer or enum goes here */)
{
  case /* potential value of the aforementioned int or enum */:
     /* code */
  case /* a different potential value */:
     /* different code */
  /* insert additional cases as needed */
  default: 
     /* more code */
  }
                                          

The Switch-Case construct takes a variable, usually an int or an enum, placed after switch, and compares it to the value following the case keyword. If the variable is equal to the value specified after case, the construct "activates", or begins executing the code after the case statement. Once the construct has "activated", there will be no further evaluation of cases.

Switch-Case is syntactically "weird" in that no braces are required for code associated with a case.

Very important: Typically, the last statement for each case is a break statement. This causes program execution to jump to the statement following the closing bracket of the switch statement, which is what one would normally want to happen. However if the break statement is omitted, program execution continues with the first line of the next case, if any. This is called a fall-through. When a programmer desires this action, a comment should be placed at the end of the block of statements indicating the desire to fall through. Otherwise another programmer maintaining the code could consider the omission of the 'break' to be an error, and inadvertently 'correct' the problem. Here's an example:

switch ( someVariable ) {
case 1:
   printf("This code handles case 1\n");
   break;
case 2:
   printf("This prints when someVariable is 2, along with...\n");
   /* FALL THROUGH */
case 3:
   printf("This prints when someVariable is either 2 or 3.\n" );
   break;
}
                                          

If a default case is specified, the associated statements are executed if none of the other cases match. A default case is optional. Here's a switch statement that corresponds to the sequence of if - else if statements above.

Back to our example above. Here's what it would look like as Switch-Case:

switch (grade)
{
   case 1:
      printf("A\n");
      break;
   case 2:
      printf("B\n");
      break;
   case 3:
      printf("C\n");
      break;
   case 4:
      printf("D\n");
      break;
   default:
      printf("F\n");
      break;
}
                                          

A set of statements to execute can be grouped with more than one value of the variable as in the following example. (the fall-through comment is not necessary here because the intended behavior is obvious)

switch ( something)
{
  case 2:
  case 3:
  case 4:
      /* some statements to execute for 2, 3 or 4 */
      break;
  case 1:
  default:
     /* some statements to execute for 1 or other than 2,3,and 4 */
     break;
} 
                                          


Switch-Case constructs are particularly useful when used in conjunction with user defined enum data types. Some compilers are capable of warning about an unhandled enum value, which may be helpful for avoiding bugs.


 

All text is available under the terms of the GNU Free Documentation License
Source : Wikibooks

 

 

 



 




   Sponsors
 

   Software
500 Java Tips E-book
PHP editor
PERL editor
Beginning Java
Beginning Visual Basic
Learn VB.net
Learn VB 6
VB and databases
ASP image library
C++ builder programming
C++ fundamentals

   Source Code
A to Z links(PHP)
Floppy disk ready(C)
show amount of lines in a multi line textbox(VB)
Get all disk drives(C Sharp)
Status bar text mouseover(Javascript)
save text to a file(VB)
wmi and soundcard(VB)
Blinking label(VB)




Copyright © 2004 by programmershelp.co.uk