Practical Application for C Programming: Switch Statements

Instructor: Vignesh Sivabalan
This lesson gives a practical exposure to switch statements in the C programming language and allows you to experiment with multiple ways of coding switch case statements.

Lesson Overview & Knowledge Required

Switch Case is a conditional statement in C programming and is used as a substitute for very long if-else statements.

  • Switch Case statements are multi-way branching statements used to design menu-like programs.
  • Switch statements allow control to one choice among multiple choices.
  • The switch expression is compared to all the case values and the code block whose case value matches is executed.

This practical lesson will check your ability to code, compile, and run the switch statements in C, using any C compiler that you are familiar with. The best choice is Turbo C/C++ compiler for compiling the C programming code. A clear understanding of if, else and else if statements will help you in coding switch statements in C. You will obtain a basic understanding of switch statements and its syntax as you move further into this practical lesson.

Program Code

As mentioned, switch statements help to develop menu-like programs where one of multiple options is chosen for execution. Let us develop a C program using switch statements that display various arithmetic operations including addition, subtraction, multiplication, and division. The main condition for the switch statement must apply hereā€”it chooses only one among the four arithmetic operations for execution.

/* Example Program */
include<stdio.h> include<conio.h>
void main()
 int x, y, z, option;
 while(option <=4)
   /* To print the choices available */
   printf('\n 1. Enter 1 to choose addition');
   printf('\n 2. Enter 2 to choose subtraction');
   printf('\n 3. Enter 3 to choose multiplication');
   printf('\n 4. Enter 4 to choose division');
   /* To take an input form the user */
   printf('\n Enter your option');
   scanf('%d', &option);
     case 1:
         printf('Enter two numbers to add');
         scanf('%d %d', &x, &y);
         z = x + y;
         printf('%d', z);
     case 2:
         printf('Enter two numbers for subtraction');
         scanf('%d%d', &x, &y);
         z = x - y;
         printf('%d', z);
     case 3:
         printf('Enter two numbers for multiplication');
         scanf('%d%d', &x, &y);
         z = x * y;
         printf('%d', z);
     case 4:
         printf('Enter two numbers for division');
         scanf('%d%d', &x, &y);
         z = x / y;
         printf('%d', z);
         printf('Enter a correct option');
         printf('press some key to continue');

Code Application

Let us do some modifications to the above switch statement program for arithmetic operations.

1. Replace the above case label values 1, 2, 3 and 4 with some negative integers like -1, -2, -3 and -4 and execute the code. You will still be able to get the desired results but you must pass a relevant option as the user's choice. The only rule is that each case value must be unique. You can also use alphabetic characters as case values.

For example:

   case -1:
       //block of code to be executed
   case -2:
       //block of code to be executed
   case -3:
       //block of code to be executed
   case -4:
       //block of code to be executed

2. Remove all of the break statements from the above example program. Compile and check the output. When break statements are removed from all of the blocks, then all consecutive code blocks get executed. This is not the desired result of a switch statement as switch should only execute one matched case and code block.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 160 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create An Account