VOOZH about

URL: https://www.geeksforgeeks.org/cpp/switch-statement-in-cpp/

⇱ Switch Statement in C++ - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Switch Statement in C++

Last Updated : 15 Jun, 2026

The switch statement in C++ is a multi-way selection statement that allows a program to execute different blocks of code based on the value of an expression. It provides a cleaner and more organized alternative to long chains of if-else-if statements when multiple conditions depend on the same expression.

  • The controlling expression must evaluate to an integral type, enumeration type, or a type that can be implicitly converted to an integral type.
  • The switch statement improves code readability and simplifies handling multiple possible values of an expression.

Output
A

Syntax

switch(expression)
{
case value1 :
// Statements
break; // break is optional

case value2 :
// Statements
break; // break is optional

default :
// Statements executed if no case matches
}

Where:

  • expression is the value that determines which case will execute.
  • case labels represent constant values that are compared against the expression.
  • break terminates the switch statement and prevents fall-through.
  • default executes when no case label matches the expression.

Working of switch Statement in C++

The working of a switch statement is as follows:

  1. The switch expression is evaluated.
  2. Its value is compared with each case label.
  3. If a matching case is found, execution begins from that case.
  4. The break statement terminates the switch block.
  5. If no case matches, the default block is executed (if present).
  6. Control moves to the statement following the switch block.

Flowchart of Switch Statement

The following flowchart illustrates how program control flows through a switch statement based on the value of the controlling expression.

👁 switch_conditional_statement

Rules of the switch Statement

When using the switch statement in C++, there are a few rules to keep in mind:

  • The case values must be of int or char type.
  • You can have as many case blocks as needed.
  • Duplicate case values are not allowed.
  • A break statement is optional in each case. However it is recommended to use it otherwise the following non-matching cases would also get executed until a break is found.

Using break in switch Statements

The break statement terminates the execution of a switch block. Without break, execution continues into subsequent cases until another break or the end of the switch statement is reached. This behavior is known as fall-through.


Output
One 

Examples

The below program demonstrates the uses of switch statement in C++ programs:

Print Day Name

The following program prints the day corresponding to a given day number.


Output
Thursday

Simple Calculator using switch

The following program performs basic arithmetic operations based on the operator entered by the user.


Output

Enter the two numbers: 10 2
Enter the Operator (+,-,*,/)
Enter any other to exit
+
10 + 2 = 12

Nested Switch Statements

A nested switch statement is a switch statement placed inside another switch statement. The inner switch is executed only when control reaches its containing case in the outer switch.


Output
Elective Courses: Machine Learning, Big Data

Explanation: The outer switch first evaluates the value of year. Since year is 2, control enters case 2, where another switch statement is executed. The inner switch then evaluates the value of branch and executes the matching case.

Enum in switch Statement

C++ also allows enumeration constants to be used in a switch statement. Using enums improves code readability and makes programs easier to maintain.


Output
Wednesday

Default Statement in switch

The default label specifies the statements that should execute when none of the case labels match the value of the switch expression. The default label can appear anywhere inside the switch block.

Example 1: Default in the Middle


Output
2
3

Example 2: Default at the Beginning


Output
Default
1

Note: The position of the default label does not affect when it is selected. It executes only when no matching case is found. However, because of fall-through behavior, statements after the default label may also execute if a break statement is not encountered.

Case Label Variations

The switch expression can be a variable or a valid expression. However, every case label must be a compile-time constant expression.

Example 1: Using an Expression in switch


Output
3

Example 2: Case Label Cannot Be a Variable

Output

error: the value of 'x' is not usable in a constant expression

Explanation: Case labels must be constant expressions known at compile time. Variables and variable expressions cannot be used as case labels.

Related Article

Difference Between switch and if-else-if in C++.

Comment
Article Tags:
Article Tags: