Switch and Nested Switch condition in c programming

 Switch statement in C programming is used when we have multiple options and we need to perform a different task for each option.

Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.

Each case in a block of a switch has a different name/number which is referred to as an identifier. The value provided by the user is compared with all the cases inside the switch block until the match is found.

If a case match is NOT found, then the default statement is executed, and the control goes out of the switch block.

Before moving forward , let’s checkout the syntax of it.

 switch (variable or an integer expression)
 {
     case constant:
     //C Statements;
     break;
     case constant:
     //C Statements;
     break;
     default:
     //C Statements;
     break;
 }
 



Now given below are the set of rules while writing switch statements
  • 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.

Flowchart of switch statement 

Break statement in Switch Case
Break statements are useful when you want your program-flow to come out of the switch body. Whenever a break statement is encountered in the switch body, the control comes out of the switch case statement.

Example without break


 int i=1;
 switch(i) 
 {
    case 1 : printf("A") ;
    case 2 : printf("B") ;
    case 3 : printf("C") ;
              break;
 }
 
Output

A B C

Example with break

 int i=1;
 switch(i) 
 {
    case 1 : printf("A") ;
                break;
    case 2 : printf("B") ;
    case 3 : printf("C") ;
                 
 }
 

Output

A


Nested switch statement

There is also nested switch loop which basically means that there is a switch statement inside another switch statement. 
Below is an example of a nested switch statement in c programming

switch (a)
{
  printf("This a is part of outer switch" );
  case 1: // code to be executed if a = 1;
    break;
  case 2: 
    switch(b) 
    {
      case 1:
        // code to be executed if b = 1;
        printf("This b is part of inner switch" );
        break;
      case 2: 
        // code to be executed if b = 2;
        printf("This b is part of inner switch" );
        break;
    }  
    break;
    default: printf("Enter valid input") ;
      // code to be executed if 
      // a doesn't match any cases
}






Post a Comment

0 Comments