Switch Statement In C Programming Language

Switch Statement In C Programming Language

With the aid of an example, you will learn how to write a switch statement in C programming in this article.

A switch case statement examines an expression that is provided and, depending on whether the evaluated value satisfies a predetermined condition, executes the related statements.

In essence, it is utilized to carry out various activities depending on various circumstances (cases).

Switch case statements allow a value to change execution control and adhere to a selection-control method.
They serve as a replacement for lengthy if clauses that compare a variable to several integral values.

A multiway branch statement is the switch statement. It offers a simple method for allocating execution to various sections of code according to the expression’s value.

The switch statement in C++ is used to execute one condition out of many conditions. It resembles an if-then-else ladder.

We can choose to execute one code block out of several options thanks to the switch statement.

The if…else…if ladder allows you to accomplish the same task. However, the syntax of the switch statement is much easier to read and write.

Switch…case syntax(switch statement in c)

switch (expression)
​{
    case constant1:
      // statements
      break;

    case constant2:
      // statements
      break;
    .
    .
    .
    default:
      // default statements
}

What’s the switch statement operation in C?

The values of each case label are taken into account once the expression has been evaluated.

If there is a match, the statements that follow the matching label are carried out. For instance, if the expression’s value equals constant2, the sentences following case constant2: are carried out until break is reached.

The default statements are executed if there is no match.

Notes: If the break statement is not used, all statements after the label that matches are also executed. The switch statement’s default clause is not required.

Example: Simple Calculator(switch statement in c)

// Program to create a simple calculator
#include <stdio.h>

int main() {
    char operation;
    double n1, n2;

    printf("Enter an operator (+, -, *, /): ");
    scanf("%c", &operation);
    printf("Enter two operands: ");
    scanf("%lf %lf",&n1, &n2);

    switch(operation)
    {
        case '+':
            printf("%.1lf + %.1lf = %.1lf",n1, n2, n1+n2);
            break;

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

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

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

        // operator 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 operation variable contains the – operator that the user entered. Additionally, the two operands 32.5 and 12.4 are kept in the corresponding variables n1 and n2.

Because the operation is -, the program’s control jumps to

printf("%.1lf - %.1lf = %.1lf", n1, n2, n1-n2);

The switch statement is finally ended by the break statement.

Leave a Reply