Switch case MCQ 10 : Shift Operators in Switch

C Programming shift operators in switch case

void main()
{
 int choice = 1 ;
 switch(choice << 1)
 {

  case 2:
        printf("Case 2");
        break;

  case 4:
        printf("Case 4");
        break;

  case 8:
        printf("Case 8");
        break;
 }
}

Explanation :

In this example we have used shift operators in switch case statements. The shift operators are of two types left shift.

int choice = 1 ;
 switch(choice << 1)
 {

In this code line we need to analyze which case label we will be executing ?

choice << 1 = Left shift Integer 1 by 1 bit
            = 0000 0000 0000 0001 << 1
            = 0000 0000 0000 0010
            = Equivalent to Integer 2
            = Case 2

In this situation, case 2 will be executed. If we shift it by 2 bits then case 4 will be executed

choice << 1 = Left shift Integer 1 by 1 bit
            = 0000 0000 0000 0001 << 2
            = 0000 0000 0000 0100
            = Equivalent to Integer 4
            = Case 4

Multiple parameters in switch case label : Switch case MCQ

In this tutorial we will be learning new way of using switch case statements. In this example we will be using Multiple parameters in switch case label in C Programming

Multiple parameters in switch case label

void main()
{
 int choice = 2 ;
 switch(choice)
 {
  case 1,2,1 :
        printf("Case 1,2,1");
        break;

  case 1,3,2 :
        printf("Case 1,3,2");
        break;

  case 4,5,3 :
        printf("Case 4,5,3");
        break;
 }
}

Output :

Case 1,3,2

Explanation

In this program we have all the cases separated with the comma, like –

case 1,2,1 :

In order to tackle such kind of statement we need to understand the Operator precedence and associativity table from chapter C programming operators.

Role of comma operator

Comma operator is usually used for separating things. See different usages of comma operators.

Comma operator returns rightmost operand and it goes on executing from left to right, so in this situation case 1,2,1 will execute like –

case 1,2,1 = Execute from L -> R
           = case 1,2,1   // Return rightmost operand from 1st & 2nd
           = case 2,1     // Return rightmost operand from 2nd & 3rd
           = case 1

In short –

CaseExplanation
case 1,2,1 It would return case 1
case 1,2,2 It would return case 2
case 1,2,3 It would return case 3
case 3,4,4 It would return case 4
case 5,5,5 It would return case 4

Misplaced continue in switch case : C Programming

Misplaced continue in switch case

Consider below program, In the below program we wrote the continue statement inside the switch case statement.

Misplaced continue in switch case Examples

Example 1 : continue statement in switch

void main()
{
 int choice = 2 ;
 switch(choice)
 {
  case 1 :
        printf("In Case 1");
        break;

  case 2 :
        printf("In Case 2");
        continue;
		
  case 3 :
        printf("In Case 3");
        break;
 }
}

Output :

Error : misplaced continue

Explanation

In the above program we have written switch case statement. break statement in switch case is used to terminate the execution of the case. It takes control outside switch statement.

case 1 :
    printf("In Case 1");
    break;

When case 1 is executed then immediately after the execution of the case 1, break statement will move control out of switch block

case 2 :
    printf("In Case 2");
    continue;

Using continue statement in switch case will throw an error because continue statement takes control to the condition checking statement again

Example 2 : continue statement in if block

int main()
{
	
int num=10;

if(num == 10)
  continue;

return 0;
}

Output :

In function 'main':
Line 7: error: continue statement not within a loop

Important rule while using continue statement is that, We can use continue statement only within the loops. Switch case is conditional block not a loop so we cannot execute the continue statement inside switch

Lookup table :

Consider the below table in which we have mentioned whether continue/break statement is allowed in control and loop statements or not.

Misplaced continue in switch case C Programming

Semicolon at the end of switch : C Programming

In this tutorial we will look into the special case of the switch case statement where we have semicolon at the end of switch case statement.

Semicolon at the end of switch :

void main()
{
 int choice = 2 ;
 switch(choice);
 {
  case 1 :
        printf("Case 1");
        break;

  case 2 :
        printf("Case 2");
        break;

  case 3 :
        printf("Case 3");
        break;
 }
 printf(" Finally I am in main ");
}

Output :

Finally I am in main

Explanation

In this example we have written a switch case and there is semicolon at the end of switch statement. In C programming all the statements ends with the semicolon.

switch(choice);

In the above statement once we write semicolon, complete block of the switch will be ignored by the compiler. It will create bodyless switch case statement like –

switch(choice) {
}

In this example the value of variable choice is equal to 2. Though value of choice is 2 we dont have anything to execute because above example can be interpreted as –

Semicolon at the end of switch

Expression in switch case

We have already studies the expression in c programming and some basic rules of switch case statement. In this tutorial we will be learning, how expression in switch case is evaluated ?

Expression in switch case

void main()
{
 int choice = 2 ;
 switch(choice)
 {

  case 1 :
        printf("Inside case 1");
        break;

  case 7-8+3 :
        printf("Inside case 7-8+3");
        break;

  case 9/3 :
        printf("Inside case 9/3");
        break;
 }
}

Output :

Inside case 7-8+3

Explanation

Using expression in switch case is absolutely allowed in c programming. Expression should be in such a way that result of expression should be integer constant

case 7-8+3 :
    printf("Inside case 7-8+3");
    break;

In this case expression evaluation will be like this –

case 7-8+3 = case 7 - 8 + 3
           = case -1 + 3
           = case 2

So we can say that case 7-8+3 expression is equivalent to case 2

Tricky examples

Example 1 : Use of variable in switch case

In this type of example, If a variable is used to evaluate expression in switch case then it may provide result which is completely dynamic and variable dependent.

case a+1 :
    printf("Inside case 7-8+3");
    break;

The case would be –

Value of aValue of case a+1Explanation
1case 2It becomes case 2 if value of variable is 1
2case 3It becomes case 3 if value of variable is 2
3case 4It becomes case 4 if value of variable is 3

We have already seen such kind of example where we have used variable in switch case statement.

Example 2 : Repeated evaluation of expression in switch case

case 7-8+3 :
     printf("Inside case 7-8+3");
     break;

case 9/3-1 :
     printf("Inside case 9/3-1");
     break;

case 7+1-6 :
     printf("Inside case 7+1-6");
     break;

In this case all the expressions are evaluated to case 2, so it is again confusing condition if we use expression in switch case.

So it is recommended to use expression in such a way that evaluation result of each expression is unique

Switch case labels in random order

In this example we are checking the results when switch case labels in random order is written inside c program

Switch case labels in random order

void main() {
   int choice = 7;

   switch (choice)
   {
   default:
      printf("I am in Default");

   case 1:
      printf("I am in case 1");
      break;

   case 2:
      printf("I am in case 2");
      break;

   case 3:
      printf("I am in case 3");
      break;
   }
}

Options : Guess the Output

  1. I am in Default I am in Case 1
  2. I am in Case 1
  3. I am in Default
  4. None of these

Explanation :

Firstly let me print output of the above program –

I am in Default 
I am in Case 1

Now if you see the above program then you can notice one thing we have choice = 7 means default case would be executed.

Compiler predicts which case we need to execute before going inside switch case, but while execution it checks each case label from top to bottom. In this situation default is written at first position so compiler goes inside the default case.

Also read Switch Case rules

Inside the default it will print following statement –

printf("I am in Default");

Once condition is checked and case statement is executed then switch case wont check the condition again, it starts executing all the remaining cases written below current case.

default:
   printf("I am in Default");
case 1:
   printf("I am in case 1");
   break;

We have written case 1 exactly after the default case so case 1 will be executed. As we have written break statement inside the case 1, it terminates the switch

If we write the above program like this by including break statement inside default case –

default:
   printf("I am in Default");
   break;
case 1:
   printf("I am in case 1");
   break;

then only statements inside default would be printed on the screen

Here is nice discussion about the effects caused over efficiency when we place switch case labels in random order