C switch ... case statement

C switch ... case statement

At the end of this chapter, you will learn switch writing statements in C programming.
Using nested if ... else statement, you can execute a particular code block against many values. But if you want to check the value of a single variable then using the switch statement instead of nested if ... else statement would be better.

The switch statement is often faster than the nested if ... else statement, and the syntax of the Switch statement is relatively simple and transparent.

Switch syntax

switch
 {
    case value1:
        If the value of // expression is equal to value1, this code will be executed;
        break;

    case value2:
        If the value of // expression is equal to value 2, this code will be executed;
        break;
        .
        .
        .
    default:
        // If the value of expression is not equal to any value, this code will be executed;

switch ... How does the case statement work?
The expression syntax above is executed only once in the program.
The value of Expression matches the case value of the Switch block, so that the program control comes to it as the code value of the case value is executed.
If the value of expression in pseudocode above is equal to value 2 then compiler will execute code block of case value2.
If there is no break statement after case value2, the Switch block will execute till the end.
Default value of the code block will be edited if the value of the expression does not match any case value of the Switch block.
The break statement is used to prevent further case from execution.
Switch Statement Flowchart

C switch ... case statement

Flowchart of Switch Statement

 

Examples 1: Switch statement
#include <stdio.h>
#include <conio.h>

int main ()
    {
    int num;
    clrscr ();
    printf ("Enter any number (1 to 7) \ n:");
    scanf ("% d", & num);
    switch (num)
    {
        case 1:
            printf ("Today is Saturday");
            break;
        case 2:
            printf ("Today is Sunday");
            break;
        case 3:
            printf ("Today is Monday");
            break;
        case 4:
            printf ("Today is Tuesday");
            break;
        case 5:
            printf ("Today is Wednesday");
            break;
        case 6:
            printf ("Today is Thursday");
            break;
        case 7:
            printf ("Today is Friday");
            break;
        default:
            printf ("Only enter value 1 to 7");
    }
    getch ();
}


Output

Enter any number (1 to 7): 4
Today is Tuesday
Expression value of the input given by user 4 in the above program matches case 4: and has been printed on Tuesday.


Examples 2:Switch statements
// Program for creating general calculator
// Based on input by user, add, subtraction, multiplication or division will be completed.

# include <stdio.h>

int main () {

    char operator;
    double firstNumber, secondNumber;

    printf ("Enter an operator (+, -, *,):");
    scanf ("% c", & operator);

    printf ("Enter two operands:");
    scanf ("% lf% lf", & firstNumber, & secondNumber);

    switch (operator)
    {
        case '+':
            printf ("%. 1lf +% .1lf =% .1lf", firstNumber, secondNumber, firstNumber + secondNumber);
            break;

        case '-':
            printf ("%. 1lf -% .1lf =% .1lf", firstNumber, secondNumber, firstNumber-secondNumber);
            break;

        case '*':
            printf ("%. 1lf *% .1lf =% .1lf", firstNumber, secondNumber, firstNumber * secondNumber);
            break;

        case '/':
            printf ("%. 1lf /% .1lf =% .1lf", firstNumber, secondNumber, firstNumber / firstNumber);
            break;

        This statement will be executed if the operator does not match any case value (+, -, *, /).
        default:
            printf ("Error! operator is not correct");
    }

    return 0;
}

Output

Enter an operator (+, -, *,): -
Enter two operands: 50.27
20.25
32.5 - 12.4 = 30.02
User - operator input has been submitted to the oparetor variant. And given the two operands 32.5 and 12.4 inputs, these were first stored in the firstNumber and secondNumber variables respectively.

Then jumped the control of the program to the bottom block.

printf ("%. 1lf -% .1lf =% .1lf", firstNumber, secondNumber, firstNumber-firstNumber);
Finally the break statement ends the statement of Switch
.

If the break statement was not used then all the subsequent cases of the correct case were executed.

Post a Comment

0 Comments