How switch Statement Work?

The working of the switch statement in C is as follows:

  1. Step 1: The switch variable is evaluated.
  2. Step 2: The evaluated value is matched against all the present cases.
  3. Step 3A: If the matching case value is found, the associated code is executed.
  4. Step 3B: If the matching code is not found, then the default case is executed if present.
  5. Step 4A: If the break keyword is present in the case, then program control breaks out of the switch statement.
  6. Step 4B: If the break keyword is not present, then all the cases after the matching case are executed.
  7. Step 5: Statements after the switch statement are executed.

We can also understand the working of the switch statement in C using the flowchart.

Switch Statement in C

Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. Basically, it is used to perform different actions based on different conditions(cases). 

  • Switch case statements follow a selection-control mechanism and allow a value to change control of execution.
  • They are a substitute for long if statements that compare a variable to several integral values.
  • The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.

In C, the switch case statement is used for executing one condition from multiple conditions. It is similar to an if-else-if ladder.

The switch statement consists of conditional-based cases and a default case.

Similar Reads

Syntax of switch Statement in C

switch(expression) { case value1: statement_1; break; case value2: statement_2; break; . . . case value_n: statement_n; break; default: default_statement; }...

How to use switch case Statement in C?

Before using the switch case in our program, we need to know about some rules of the switch statement....

How switch Statement Work?

The working of the switch statement in C is as follows:...

Flowchart of Switch Statement

Flowchart of switch statement in C...

Break in switch case

This keyword is used to stop the execution inside a switch block. It helps to terminate the switch block and break out of it. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement....

Default in switch case

The default keyword is used to specify the set of statements to execute if there is no case match....

Important Points About Switch Case Statements

1. Switch expression should result in a constant value...

Examples of switch Statement in C

Example 1:  C Program to print the day of the week using a switch case....

Advantages of C switch Statement

Easier to read than if else if.Easier to debug and maintain for a large number of conditions.Faster execution speed....

Disadvantages of C switch Statement

Switch case can only evaluate int or char type.No support for logical expressions.Have to keep in mind to add a break in every case....

Conclusion

In this article, we discussed the switch statement in C programming and how to use it. It is a conditional statement like the if-else-if ladder having its own merits and demerits. It is mostly preferred when the number of conditions to evaluate is large....

FAQs on C switch Statement

1. What is the switch case in C?...