View Course Path

Switch Case statements in C – Full explanation with examples and tutorials

What is the first thing that comes to your mind when you think of a switch statement? Probably something that switches. Right? Well, if there is something that switches this means that there is a switch? Concur? Okay, if things are switching that means there is a control mechanism. True? So in a switch statement or a switch case statement in C, we can have a number of cases which we can switch to (The switch statement provides the controlling mechanism) using a switch (which can be an integral value). We will look at a more formal definition in the upcoming sections below.

Integral Value
An integral value can be any of the following: 1. An integer (signed or unsigned) 2. A character 3. Enumerated bits. Check out this article to understand what integral values in programming means.

What is the syntax of switch-case statements in C?

I will present two syntaxes. The first one will be of a type you will most commonly use and see. The second one will be a detailed version of the switch statement syntax which can be interpreted according to your usage basis.

switch (n)
{
    case 1: // code to be executed if n = 1;
        break;
    case 2: // code to be executed if n = 2;
        break;
    default: // code to be executed if n doesn't match any case
}

General Syntax

switch(expression) 
{
case constant-expression :
case to be executed if constant expression matches the expression value;
break; // optional

case constant-expression :
case to be executed if constant expression matches the expression value;
break; /* optional */

/* you can have any number of case statements */
default : //Statement to execute if expression value does not match any cases. Optional.
statement(s);
}

What is a switch case statement? How do these switch statements work?

A switch case statement is a multiway branch statement that allows a user to pass control of the execution of the program to certain blocks. It is generally used to replace lengthy if-else-if statements. Not that anything is different in terms of application between the two, but switch-case statements are simpler to write and read. A switch statement takes an integral expression (an int, char, long or enum) and depending on the integer value of the expression the appropriate case executes. If the integral expression of the switch statement does not match any case expression, the default case executes.

Rules and properties of the switch-case statement in C

  • The switch expression should be in brackets ().
  • The break; statements are optional. If you choose to not use it, the subsequent case after the selected case will execute. If you wish to strictly restrict execution only to the chosen case, then you should use break statements.
  • Every case expression should be unique.
  • The default statement is executed in case a wrong switch expression is inserted into the switch-case statement.
  • Constant expressions are allowed in switch expression. Example:
    switch(1+2+23)
    switch(1*2+3%4)
    
  • Variable expressions are also allowed provided they are assigned some value. Example:
    switch(a*b+c*d)
    switch(a+b+c)
  • Break statements are used to exit a case.
  • Switch statements can be nested inside each other.

What does the flow chart of a switch case statement in C look like?

switch-case statement flowchart in C

Write a program in C using switch case statements to take numbers up to 10 from a user and print it in words.

//take numbers upto 10 from user and print it in words
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,j;
printf("Enter a number between 0 and 10\n");
scanf("%d",&i);
j=i+1;
switch(j)
{
case 1: printf("zero");
break;
case 2: printf("one");
break;
case 3: printf("two");
break;
case 4: printf("three");
break;
case 5: printf("four");
break;
case 6: printf("five");
break;
case 7: printf("six");
break;
case 8: printf("seven");
break;
case 9: printf("eight");
break;
case 10: printf("nine");
break;
case 11: printf("ten");
break;
default: printf("This is not a valid entry");
}
getch();
}

Output and Explanation

Test out this code in any IDE. If you are looking for something quick then check out any of these online IDEs and observe how the program executes.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.