Switch Case Statement in C++



Introduction

The switch statement is a control statement in programming (like C, C++, Java) that is used to execute one block of code out of many options based on the value of a variable or expression.

Instead of writing many if–else conditions, we use a switch statement when we need to compare one variable with multiple fixed values.

Why we use switch statement?

  • Makes code cleaner and easier to read

  • Better than long if–else ladder

  • Useful when choices are fixed values (menu, days, options, etc.)

Basic syntax (C / C++)

switch(expression)
{
    case value1:
        // code
        break;

    case value2:
        // code
        break;

    case value3:
        // code
        break;

    default:
        // code if no case matches
}

How switch statement works

  1. The expression is evaluated once.

  2. Its value is compared with each case.

  3. When a match is found, that case block is executed.

  4. break stops execution and exits the switch.

  5. If no case matches, the default block runs.


The example below uses the weekday number to calculate the weekday name:

int day = 3;

switch(day)
{
    case 1:
        cout << "Monday";
        break;

    case 2:
        cout << "Tuesday";
        break;

    case 3:
        cout << "Wednesday";
        break;

    default:
        cout << "Invalid day";
}

Output:

Wednesday

Try hereπŸ‘‡



Important Points

>case value must be constant.

>break is important (otherwise next cases will execute).

>default is optional but recommended.

>Works with int, char, enum (not with float or string in C++).

The break Keyword




The break keyword is used to immediately stop the execution of a loop or a switch statement and transfer control outside of it.

Example (with break)

int num = 2; switch(num) { case 1: cout << "One"; break; case 2: cout << "Two"; break; case 3: cout << "Three"; break; default: cout << "Invalid"; }

Output:

Two



Example (without break)

int num = 2; switch(num) { case 2: cout << "Two "; case 3: cout << "Three "; }

Output:

Two Three

πŸ‘‰ Because there is no break, execution continues to the next case.



The default Keyword





It executes a block of code when none of the case values match the expression.


How default works

  • The switch expression is checked against all case values

  • If no match is found, the default block executes

  • It is optional, but highly recommended

Example

int choice = 5; switch(choice) { case 1: cout << "Option 1"; break; case 2: cout << "Option 2"; break; case 3: cout << "Option 3"; break; default: cout << "Invalid option"; }

Output:

Invalid option


Important points

  • default runs only when no case matches

  • It can be written anywhere inside switch (usually at the end)

  • break is not mandatory for default if it is the last block

  • Helps handle unexpected or wrong input














Comments

Popular posts from this blog

C++ Programming for Beginners – Step by Step Guide

If-Else Statement in C++

Variables and Data Types in C++