Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

C Programming switch...case Statement

In this tutorial, you will learn to put a switch Statement in C programming with the help of an example.
The nested if...else statement allows you to execute a block code among many alternatives. If you are checking on the value of a single variable in nested if...else statement, it is better to use switch statement.
The Switch Statement is often faster than nested if...else (not always). Also, the syntax of switch statement is cleaner and easy to understand.


Syntax of switch...case
switch (n)
{
    case constant1:
        // code to be executed if n is equal to constant1;
        break;

    case constant2:
        // code to be executed if n is equal to constant2;
        break;
        .
        .
        .
    default:
        // code to be executed if n doesn't match any constant
}
When a case constant is found that matches the switch expression, control of the program passes to the block of code associated with that case.
In the above pseudo code, suppose the value of n is equal to constant2. The compiler will execute the block of code associate with the case statement until the end of switch block, or until the break statement is encountered.
The break statement is used to prevent the code running into the next case.




Example: switch Statement
// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division depending the input from user

# include

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;

        // operator is doesn't match any case constant (+, -, *, /)
        default:
            printf("Error! operator is not correct");
    }

    return 0;
}
Output
Enter an operator (+, -, *,): -
Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
The - operator entered by the user is stored in operatorvariable. And, the two operands, 32.5 and 12.4 are stored in variable firstNumberand secondNumber respectively.
Then, control of the program jumps to
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber);
Finally, the break; statement ends the switch statement.



This post first appeared on C-Programming, please read the originial post: here

Share the post

C Programming switch...case Statement

×

Subscribe to C-programming

Get updates delivered right to your inbox!

Thank you for your subscription

×