Switch Case Statement in C



Syntax 
    switch(Variable or Value)
    {
        case Value1:
                    Statement Block For Value1;
                    break;
        case Value2:
                    Statement Block For Value2;
                    break;
        ............
        ............
        case ValueN:
                    Statement Block For ValueN;
                    break;
        default:
                Default Statement Block For No Match;
    }

switch...case statement is useful for providing list of options to the user from which user can select any one option.
Using switch....case statement we can represents multiple options to the user so it is also known as Multiple Choice Statement. It is also known as alternative of if...else if ladder statement.

     

In switch...case statement value or value of the variable is compared with each case value that is specified inside switch statement. If any case value match with the value provided in switch statement the Statement block associated with that case is executed.
If none of the case value match with value specified inside switch then default statement block is executed.

Example
switch (ch)
{
    case 1:
           printf("a + b = %d", a + b);
           break;
    case 2:
           printf("a - b = %d", a-b);
           break;
    case 3: 
           printf("a * b = %d ",a*b);
           break;
    case 4:
           printf("a / b = %d", a/b);
           break;
    default:
           printf("Wrong Choice");
}

Download Projects


Download Programs