VOOZH about

URL: https://www.geeksforgeeks.org/c/c-switch-statement/

⇱ Switch Statement in C - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Switch Statement in C

Last Updated : 7 Nov, 2025

C switch statement is a conditional statement that allows you to execute different code blocks based on the value of a variable or an expression. It is often used in place of if-else ladder when there are multiple conditions.


Output
Case 1 is Matched.

Explanation: In this program, the switch statement evaluates the variable var. Since var is 1, the code in case with value 1 executes, printing "Case 1 is Matched." and then exiting the switch block due to the break statement. Other cases and the default case are skipped.

Syntax of switch

Here,

  • expression: Expression or variable whose value is tested. It should result in an integer or character value.
  • case: Each switch case represents a block of code that will be executed with the expression evaluates to its corresponding value. There should be atleast one case in the switch.
  • value1, value2: These are the possible values of the expression for which the corresponding cases are executed. These values must be unique.
  • break: This is used to exit the switch statement once a case is executed. Without break, the program will continue executing the subsequent cases (this is called "fall through").
  • default: This block of code is executed if none of the case values match the expression. It is optional to add this block.

Flowchart of C switch Statement

👁 switch case in c

Working of Switch Statement

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

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

Examples of Switch Statement

The below programs show some use cases of switch statement in practical scenario:

Print Day Name of the Week


Output
Tuesday

Simple Calculator using Switch Statement


Output

Enter the Operator (+,-,*,/)
+
Enter the two numbers: 10 20
10 + 20 = 30

C switch Statement without Break

The breakkeyword is used to stop the execution inside a switch block. It helps to terminate the switch block and break out of it. If omitted, execution will continue on into the next case.


Output
Case 2 is executed.
Case 3 is executed.
Case 4 is executed.

Nested switch Statement

Nesting of switch statements is allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes the program more complex and less readable.


Output
Category 1 selected
Option 2 selected under Category 1
Comment
Article Tags: