Dev Frank
4 min readJan 6, 2024

SWITCH STATEMENT IN C

Switch is a keyword and a conditional statement in C language.

It is a replacement to long else if statements or constructs.

For instance, let’s take a program:

#include <stdio.h>

int main(void)
{
int x == 2;

if(x == 1)
printf("x is 1\n");

else if (x == 2)
printf("x is 2\n");

else if ("x == 3")
printf("x is 3\n")

else
printf("x is a value other than 1,2,3\n");


return (0);
}

Here writing this else if statement is very cumbersome. So here is a good replacement using switch statement.

#include <stdio.h>

int main(void)
{
int x = 2;

switch(x)
{
case 1: //case label
printf("x is 1\n");
break;

case 2:
printf("x is 2\n");
break;

case 3:
printf("x is 3\n")
break;

default :
printf("x is a number other than 1,2,3\n");
}

return (0);

}


HOW DOES A SWITCH STATEMENT WORKS?

First it checks if x is equals to one or not. That means whatever is there inside this switch will be compared with Label 1.

All the cases are checked from top to bottom. It first checks if x is equals to one, if it’s true it prints the statement (x is 1), otherwise it checks if x is equals to two or not. If x is true l, the printf statement ( x is 2) will be printed and so on.

Now suppose none of the cases are satisfied then the default statement will be printed.

NB :- Default optional.

Let’s say x is equal to 1, the statement (x is 1) will be printed. After that you can see there is a statement called break.

If x == 1 condition is satisfied and there is no break after printf, then subsequent expression will also get evaluated until we reach the next break.

In as much as x is not equal to 1, if there’s no break in case 1, the printf statement will be evaluated until there is a break.

Therefore it is important to put break immediately after the printf statement if you want to get outside the switch construct after evaluating a particular statement.

Break is also optional. It is not necessary to put break immediately after the expression.

SYNTAX OF SWITCH STATEMENT

switch (expression or variable) {
case constant1:
// code block executed if expression equals constant1
break;
case constant2:
// code block executed if expression equals constant2
break;
// additional cases as needed
default:
// code block executed if expression does not match any case
}
  • The switch expression undergoes a single evaluation.
  • The expression value is compared against each case.Upon a match, the corresponding code block is executed.
  • Implementation stops when encountering the break statement within the switch block.
  • Optionally, a default statement provides code to run when no case matches the expression.

KEY POINTS ABOUT SWITCH STATEMENT

  1. You are not allowed to add duplicate case
#include <stdio.h>

int main() {
int choice = 2;

switch (choice) {
case 1:
printf("Executing code for case 1\n");
break;
10 :14
case 2:
printf("Executing code for case 2\n");
break;

case 3:
printf("Executing code for case 3\n");
break;

19 : 14
case 2: // Duplicate case
printf("Executing code for case 2\n")
break;

default:
printf("Executing default code\n");
break;
}

return 0;
}

In this example, the switch statement has a duplicate case labeled as case 2. When this code is compiled and run, it will generate a warning about the duplicate case.

prog.c:19:14 error: duplicate case value   
case 2: printf("Executing code for case 2\n");
^

prog.c:10:14 error: previously used here
case 2: printf("Executing code for case 2\n");

In this case, the compiler is warning you about having a duplicate case with the value '2’. It’s crucial to address such warnings, as duplicate cases can lead to unpredictable behavior in your program.

2. Only those expressions which result to an integral constant value are allowed in switch statement.

NB:  A break statement was omitted 
after the default block

3. Float value is not allowed as a constant value in case label.

NB:  A break statement was omitted 
after the default block

4. Arithmetic expression that leads to an integral constant value.

NB: A break statement was omitted
after the default block

Although macros are allowed.

5. The default block can be placed anywhere inside switch. It will still get evaluated if no match is found.

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.