switch case in C/C++, A Brief Explanation

switch case in c

Using the switch case in C language, we can resolve complex conditional and branching operations. It reduced the comparison of integral value in a long if-else statement. The switch expression transfers the control to a specific part of the code which has written in the body of switch and case statements.

The control transfer capability of switch case provides an easy way to dispatch execution to different parts of code based on the value of the expression.

Syntax of switch case in C:

Flow diagram of switch case

flow diagram of switch case

How to switch and case statements work?

In switch statement when you are passed and integral argument then it searches the case constant-expression within its body. If the integral value matches with the case statement then control directly jump to the case statement and execute the code until the end of the body or break statement, break statement transfer the control out of the body.

In switch case, we can use the break statement to end the execution of the code for the particular case. If we forgot to put the break statement after each case, the program continues to the next case until not getting a break or end of the code.

Note: The best feature of the switch and case statement we can include any number of the case but need to remember no case within the same switch statement have the same value.

switch c

In this example, Every case has the break statements.If iChoice is equal to 1, the addition of two number is performed. After the execution of case 1, control comes to the break statement, it transfers the control out of the body, bypassing the remaining statements. Similarly, if iChoice is equal to 2, the subtraction of two number happens.


If you want to learn more about the c language, here 10 Free days C video course for you.

C tutorial


Rules of Using Switch Case in C/C++ Programming

  • In switch case value of each case should be integral (not- floating).
  • The value of each case must be unique within the switch- case body but their sequence does not affect the execution.
  • Using the macro Identifier, we can create case labels but it should be a unique and integral type.
  • The case labels must end with (:) colon.
  • Every switch case should have one default label. If we remove the default label and there is no case match found then no statements are executed. It creates confusion.
  • We can write default statement anywhere in the body of switch case and also not necessary to put the break statement after the default statement. But for precaution always put the break statement after the default label.
  • In some scenario, many switch cases share only one break statement.
  • There is no meaning to use comparison operator in the switch because if the condition is true, return 1 either return 0.
  • The switch case also allowed to create the nested switch case in C/C++ language.

Some important programming example

Example 1.

If there is no break statement implement within the body of the switch case.

switch case in c example

switch c code

In above example, if iChoice is equal to 1, all statements of the body are executed because there is no break statement appear in the body of the switch case. if ichoice equal to 2 then control jump to case 2 and execute all the below case since there is no break statement.

Example 2.

A single statement can carry multiple case labels.

In above example, if iChoice is equal to the 1,2 or 3, TesFunction is executed.

Example 3.

If switch case has the same case label.

case statement in c

Try Grammarly, an award-winning Writing Tool. It helps you to secure better grades. Grammarly Writing Support

Example 4.

If switch case value is floating number.

switch case float

Example 5.

We can put default case anywhere within the body see below example program.

switch statement default case

Example 6.

Case label should be a constant value in the switch case.

switch tutorial

Example 7.

In this example, I am implementing switch within the switch. When we create a switch within a switch then it’s called nested switch.

Leave a Reply