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

Use of variable in switch case

In this tricky questions, we are using the variable in switch case statement. Tutorial will explain the behaviour of program when we use variable in switch case statement

Use of variable in switch case

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

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

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

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

Options for above question :

  1. Variables are not allowed in Case Labels
  2. Default is absent
  3. No continue statement in Statement
  4. All Cases in switch are not unique

Explanation

Above program will throw an error because these following two reasons –

  1. Variables are not allowed in case labels
  2. All cases in switch are not unique

Use of Variable in switch case

We will discuss these reasons one by one –

Reason 1 : Variables not allowed

Consider that you have used variable in switch case as switch label

case a :
        printf("I am in case A");
        break;

We know that when we use variable name then the value of variable can be modified at any moment of time. Suppose at particular time value of the variable is 2 then the case inside switch become case 2 and suppose the value of variable becomes 3 then the case becomes case 3

In short the value of variable may alter the case definition so it is recommanded not to use variable in switch case statement

Reason 1 : Variables not allowed

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

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

Consider the above program where we have two same cases so it may confuse compiler which case to execute so it will throw an compile error in c program

Recommended Article : Rules of using switch case statement

Switch case outside statement

In this tutorial we will be learning how switch case outside statement is written in switch case.

C Switch case outside statement

Consider the below program, if you observe program carefully then you would understand that first printf statement is switch case outside statement

int main()
{
 int choice = 1 ;
 
 switch(choice)
 {
        printf("\nFirst Statement");

  case 1 :
        printf("\nInside Switch case 1");
        break;

  case 2 :
        printf("\nInside Switch case 2");
        break;

  case 3 :
        printf("\nInside Switch case 3");
        break;
 }
 
 return(0);
}

Output :

Inside Switch case 1

Explanation

Before reading the solution keep important switch case rule in mind that – Every Statement in switch must be wrapped within any of the case.

Recommended Article : Rules of writing switch case statement

In the above example,

printf("\nFirst Statement");

is nothing but switch case outside statement, so it is not considered as a part of any of the switch case so it is neglected by compiler

Switch case outside statement c programming

Above picture explains everything, You would be able to identify the blocks which are not the part of any case blocks

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

Index : C Programming : Question Bank (Check Your Skill)

C Programming : Challenging Questions 
[ Checking your C knowledge ]


This Section Covers all Verities of Basic Syntax , Trick ,Tactics and Different Challenging Questions . If You have any Simple Solution or Explanation or any New Problem then feel free to contact : pripat2001@gmail.com


“The greatest challenge to any thinker is stating the problem in a way that will allow a solution”

1. Scope of Variable

    4 . Input/Output :

    Bookmark & Share