Switch Case Statement in C - C Tutorial

Programming languages or concepts
0

  The switch case statement is used when we have multiple options and we need to perform a different task for each option.


Switch Case Statement in C

In the switch statement, we compare the condition value with multiple cases. When there is a match with any one of the cases, the block of code corresponding with that case is executed. Each case has a name or a number which is known as its identifier. If none of the cases matches the condition, the block of code corresponding to the default case is executed.

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.


A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.


switch (variable or an integer expression)

{

     case constant:

     //C Statements

     ;

     case constant:

     //C Statements

     ;

     default:

     //C Statements

     ;

}


How does the switch statement work?

The expression is evaluated once and compared with the values of each case label.


If there is a match, the corresponding statements after the matching label are executed. For example, if the value of the expression is equal to constant2, statements after case constant2: are executed until break is encountered.

If there is no match, the default statements are executed.


The following rules apply to a switch statement −


  • The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
  • You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


Let's see a simple example of c language switch statement.

int main()

{

      int number ;

    printf("enter a number:");    

scanf("%d",&number);    

  

    switch (number) {

    case 1:

        printf(" Your Choice is 1");

        break;

    case 2:

        printf("Your Choice is 2");

        break;

    case 3:

        printf("Your Choice is 3");

        break;

    default:

        printf("Not Available");

        break;

    }

    return 0;

}


Output:

enter a number:2

Your Choice is 2


C Switch statement is fall-through

In C language, the switch statement is fall through; it means if you don't use a break statement in the switch case, all the cases after the matching case will be executed.


The break Keyword

When C reaches a break keyword, it breaks out of the switch block.

This will stop the execution of more code and case testing inside the block.

When a match is found, and the job is done, it's time for a break. There is no need for more testing.


Recommended Articles:

Post a Comment

0Comments

Post a Comment (0)
close