From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/SYNTAX/switch.html | 233 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 reference/C/SYNTAX/switch.html (limited to 'reference/C/SYNTAX/switch.html') diff --git a/reference/C/SYNTAX/switch.html b/reference/C/SYNTAX/switch.html new file mode 100644 index 0000000..7fb870f --- /dev/null +++ b/reference/C/SYNTAX/switch.html @@ -0,0 +1,233 @@ +switch/case keywords + + + + + + +
+
+

The 'switch' and 'case' keywords

+
+
+

+ +The switch-case statement is a multi-way decision statement. Unlike the +multiple decision statement that can be created using +if-else, the switch +statement evaluates the conditional +expression and tests it against +numerous +constant +values. The branch corresponding to the value that the +expression matches is taken during execution. +

+The value of the expressions in a switch-case statement must be an ordinal +type i.e. +integer, char, short, long, +etc. Float and double are not +allowed. +

+The syntax is : +

+

+ + + + +
+
+
+     switch( expression )
+     {
+        case constant-expression1:	statements1;
+        [case constant-expression2:	statements2;]    
+        [case constant-expression3:	statements3;]
+        [default : statements4;]
+     }
+
+
+
+

+ +The case statements and the default statement can occur in any +order in +the switch statement. The default clause is an optional clause +that is +matched if none of the constants in the case statements can be matched. +

+Consider the example shown below: +

+

+ + + + +
+
+
+     switch( Grade )
+     {
+        case 'A' : printf( "Excellent" );
+        case 'B' : printf( "Good" );
+        case 'C' : printf( "OK" );
+        case 'D' : printf( "Mmmmm...." );
+        case 'F' : printf( "You must do better than this" );    
+        default  : printf( "What is your grade anyway?" );
+     }		
+
+
+
+

+ +Here, if the Grade is 'A' then the output will be +

+

+ + + + +
+
+     Excellent
+     Good
+     OK
+     Mmmmm....
+     You must do better than this    
+     What is your grade anyway?
+
+
+
+

+ +This is because, in the 'C' switch statement, execution continues on into +the next case clause if it is not explicitly specified that the execution +should exit the switch statement. The correct statement would be: +

+

+ + + + +
+
+
+     switch( Grade )
+     {
+        case 'A' : printf( "Excellent" );
+                   break;
+
+        case 'B' : printf( "Good" );
+	           break;
+
+	case 'C' : printf( "OK" );
+	           break;
+
+        case 'D' : printf( "Mmmmm...." );
+	           break;
+
+	case 'F' : printf( "You must do better than this" );    
+	           break;
+
+	default  : printf( "What is your grade anyway?" );
+                   break;
+	}		
+
+
+
+

+ +Although the break in the default clause (or in general, after +the last +clause) is not necessary, it is good programming practice to put it in +anyway. +

+An example where it is better to allow the execution to continue into the +next case statement: +

+

+ + + + +
+
+
+     char Ch;
+     .
+     .
+     switch( Ch )
+     {
+       			/* Handle lower-case characters */ 
+        case 'a' :
+        case 'b' :
+       	      .
+	      .
+	      .
+        case 'z' :
+          printf( "%c is a lower-case character.\n", Ch );
+	  printf( "Its upper-case is %c.\n" toupper(Ch) );      
+	  break;
+
+       			/* Handle upper-case characters */
+	case 'A' :	
+	case 'B' :
+	      .
+	      .
+	      .
+	case 'Z' :
+           printf( "%c is a upper-case character.\n", Ch );
+	   printf( "Its lower-case is %c.\n" tolower(Ch) );
+	   break;
+
+       			/* Handle digits and special characters */   
+
+        default  :
+           printf( "%c is not in the alphabet.\n", Ch );
+	   break;
+
+     }
+     .
+     .
+
+
+
+

+ +


+

Example:

+ +Basic switch example. +
+
+

See also:

+ + +

+ +


+

+

+ + + + +
+ Top + + Master Index + + Keywords + + Functions +
+
+

+


+
Martin Leslie +

+ + -- cgit v1.2.3-54-g00ecf