c
C Programming Tutorial : Introduction

C Conditional Statements - Switch Case Statement

This is a multi-directional conditional control statement. Sometimes there is a need in program to make a choice among number of alternatives. For making this choice, we use the switch statement. This can be written as:-

Syntax

switch(expression)
{
    case constant l:
        statement
        ……...  ..
    case constant 2:
        statement
        ……...  ..
    case constant N:
        statement
        ……...  ..
    default
        statement
        ……...  ..
}
 
Switch-Statement p size

Here switch, case and default are keywords. The “expression” following the switch keyword can be any C expression that generates an integer value. It can be value of any integer or character variable, or a function call that returning an integer, or an arithmetic, logical, relational, bitwise expression that generates an integer.

It can be any integer or character constant also. Since characters are converted to their ASCII values, so we can also use characters in the expression. Data types long int and short int are also a allowed. The constants following the case keywords should be of integer or character type. They can be either constants or constant expressions. These constants must be different from one another.

We can’t use floating point or string constants. Multiple constants in a single case are not allowed; each case should be followed by only one constant. Each case can be followed by any number of statements. It is also possible that a case has no statement under it. If a case is followed by multiple statements, then it is not necessary to enclose them within pair of curly braces, but it is not an error if we do so.

The statements under case can be any valid C statements like if else, while, for or even another switch statement. Writing a switch statement inside another is called nesting of switches. Now we’ll see some valid and invalid ways of writing switch expressions and case constants.

int a, b, c; char d, e; float f;

Valid

switch(a) switch(a>b) switch (d +e-3) switch(a>b && b>c) switch (func(a, b))’

Invalid

switch(f) switch(a+4.5)

Valid

case 4: case ‘a’: case 2+4: case ‘a’>’b’:

Invalid

case “second”: case 2.3: case a: case a>b: case a+2: case 2, 4, 5: case 2 : 4 : 5 :

Now let us see how the switch statement works.
Firstly, the switch expression is evaluated, then the value of this expression is compared one by one with every case constant.
If the value of expression matches with any case constant, then all statements under that particular case are executed.
If none of the case constant matches with the value of the expression then the block of statements under default is executed. ‘default’. is optional, if it is not present and no case matches then no action takes place. These cases and default can occur in any order.

Example-1

/*Program to understand the switch control statement*/
#include<stdio.h>
#include<conio.h>

Void main ()
{
    int choice;
    printf ("Enter your choice ");
    scanf("%d",&choice);
    switch(choice)
        {
            case 1:
                printf("First\n") ;
            case 2:
                printf("Second\n");
            case 3:
                printf("Third\n");
            default:
                printf ("Wrong choice\n");
        }
	getch();
}

Output:
Enter your choice: 2
Second
Third
Wrong choice
 

Here value of choice matches with second case so all the statements after case 2 are executed sequentially. The statements of case 3 and default are also executed in addition to the statements of case 2. This is known as falling through cases.

Suppose we don’t want the control to fall through the statements of all the cases under the matching case, then we can use break statement. If a break statement is encountered inside a switch, then al the statements following. break is not executed and the control jumps out of the switch. Let’s rewrite the above program using break.

Example-2

/*Program to understand the switch with break statement*/

#include<stdio.h>
#include<conio.h>

void main ()
{
    int choice;
    printf ("Enter your choice ");
    scanf("%d",&choice);
    switch(choice)
        {
            case 1:
                printf("First\n");
                break;
            case 2:
                printf("Second\n");
                break;
            case 3:
                printf("Third\n");
                break;
            default:
                printf("Wrong choice\n”);
        }
    getch();
}

Output:
Enter your choice 2:
Second:
 

Example-3

/*Program to perform arithmetic calculate ions on integers */
#include<stdio.h>
#include<conio.h>

void main ( )
{
    char op;
    int a,b;
    printf ("Enter number operator and another number ");
    scanf ("%d%c%d", &a, &op, &b);
    switch(op)
        {
            case '+':
                printf ("Result = %d\n", a+b) ;
                break;
            case '-':
                printf ("Result = %d\n", a-b);
                break;
            case '*'.
                printf("Result %d\n",a*b);
                break;
            case 'I':
                printf(“Result %d\n",a/b);
                break;
            case '%':
                printf("Result %d\n",a%b);
                break;
            default:
                printf ("Enter valid operator\n");
        }/*End of switch*/
    getch();    
}/*End of mainl )*/

Output:
Enter number operator and another number: 2+5
Result = 7