Turn Desktop View Off
You are here:   HomeC Programming (Theory)Control Structures in CWrite a C program to print the reverse of digit.
by Dinesh Thakur Category: Control Structures

The switch statement allows us to test an expression and depending on its value, execute a statement (or group of statements) amongst several alternatives. It is the most involved statement provided in C language. It uses four keywords, namely, switch, case, default and break. The last two keywords are optional and can be omitted. The general form of this statement is given below and its flowchart is shown in.

switch ( expr )
{
    case value1 :
    statement;
    statement;
    …………..
    break;
  case value2:
   statement;
   statement;
  break;
   ………….
 default:
   statement;
   statement;
  break;
}

The test expression expr is written within parentheses after the switch keyword. The value of this expression is used to select one of the alternative groups of statements (cases), written using the case and default keywords, for execution. Note that the test expression must be an integral expression. A beginner often makes the mistake of using either a floating or a string expression as a test expression in a switch statement.

          Flowchart for the switch statement

The body of the switch statement consists of one or more alternative groups of statements each preceded by a line of the form

case value:

Only one value can be written after each case keyword and it must be a constant integral expression. Thus, we can use integer and character constants as case values. However, floating and string constants are not permitted. Also, we cannot write non-constant expressions, numbers separated by commas or spaces (such as 1, 2, 3) and ranges (such as (0 ... 100, 'A' ... 'Z', etc.) as case values.

Each alternative statement group may contain one or more statements. We can include any valid statement in this group. Each alternative statement group is usually followed by an optional break statement. The statements in a particular group are selected for execution if the case value specified before that group equals the value of the test expression expr. Note that the switch statement may also contain one group of alternative statements preceded by a line of the form

default:

This statement group is optional. However, if specified, the statement(s) within this default group are executed if the value of expr is not found in any of the case clauses. Although we can write this group anywhere in a switch statement, it is usually written as the last group.

The execution of the switch statement proceeds as follows: Initially, expression expr is evaluated and its value is compared with the case values value], value2, ... valueN in the order in which they are specified in the switch statement. If the value of expression expr matches a case value, the corresponding statement group is executed. If a break statement is encountered during this, the control leaves the switch statement; otherwise, the control falls through the next case and executes the statements in that case.

Note that if the value of expr does not match any of the case values specified in the switch statement, the statement group associated with the default case is executed if it is present; otherwise, the control leaves the switch statement without executing any statement.

Although individual case values and default group can be written in any order, it is a good practice to mention case values in some order and the default at the end of a switch statement. It is also a good programming practice to write a break statement after the last group of alternative statements. Although not really required, it prevents the introduction of a bug in the program when we add more cases at the end of the switch statement. Finally, note that we can associate a group of statements with more than one case values by listing these case values before that statement group as shown below:

case value1:

case value2:

…………..

case valueK:

        statement;

        statement;

       break;

These statements will be executed whenever expr evaluates to any one of the values value1, value2, ... , valueK. Note that this behavior of the switch statement is possible because in the absence of a break statement, the control falls through to the next case.

The following points must be taken care of while using switch statements:

(i) The switch (expression) as well as case value expressions must evaluate to integral values. Characters may also be used because these are also integral constants according to ASCII code.

(ii) All the cases (choices) are enclosed between a pair of curly brackets. Each case value is followed by a colon ( : ).

(iii) If a key matches with a case value, all the statements following a match are carried out.

(iv) In case of multiple statements after colon(:) there is no need to enclose them in curly braces.

(v) Every case must end with break; statement, otherwise statements in all the cases following a match would be carried out

(vi) No two cases should have identical case values..

Illustrates switch statement which tells you the fare for traveling to a city

#include<stdio.h>
 
void main()
 
{
 
   char ch;
 
   clrscr();
 
   printf("Enter a letter for the station you want to go to.\n");
 
   printf("For Delhi enter D\n"
 
       "For Mumbai enter M\n"
 
       "For Chennai enter C\n"
 
       "For Kolkata enter K\n");
 
   scanf("%c", &ch);
 
   switch (ch) //start of switch statement
 
   {
 
     case 'D':
 
        printf("Fare to Delhi is Rs.5000.\n");
 
        break ;
 
    case 'M':
 
       printf("Fare to Mumbai is Rs.6000.\n"); break;
 
    case 'C' :
 
      printf("Fare to Chennai is Rs.8000.\n"); break;
 
    case 'K':
 
      printf("Fare to KolKata is Rs.6500.\n"); break;
 
    default:
 
     printf("The entry is not in the list.\n");
 
  }
 
}

switch Statement in C



Subscribe To Free Daily Newsletter!

Get Free News Updates Delivered Directly To Your Inbox
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to contact us.



What's New and Popular





Popular Article