Why choose switch case in C++

By | December 16, 2022

In this technological and digital age where everything is automated. We should prefer something which saves time and space and makes our work easy. Lucky for us C++ programmers, we have switch case statements. It is a great alternative to the If else condition statement. It reduces the amount of code to a very small size which saves us time in writing the code and makes it easier for us to understand. It comes with the advantage of adding more posts. So, adopting this statement will be a game changer for advanced coders as well as beginners.

Use of switch case and its syntax:

Switch case is generally used in a situation where we need to consider many conditions, outcomes, and values of a variable or an expression and act according to each condition.

Illustration:

Syntax:

switch (expression / variable)

{

case value1:

  // Code to be executed if expression/variable==value1.

  break;

case value2:

  // Code to be executed if expression/variable==value2.

  break;

default:

  // Code to be executed 
  // if expression/variable does not match any of the above cases.

}

Don’t get it wrong, it is not limited to only 2 terms but is infinite. You can add as many conditions as you want.

Downfall of switch statement:

  1. It doesn’t work well with floats and strings .
  2. It is a frequent source of bugs and glitches.

A simple switch case program:

#include <iostream>

using namespace std;

int main() {

    char c;
    cout << "Enter an alphabet: ";
    cin >> c;

    switch (c) {

    case 'a':
      cout << "It is a vowel - a" << endl;
      break;

    case 'e':
      cout << "It is a vowel - e" << endl;
      break;

    case 'i':
      cout << "It is a vowel - i" << endl;
      break;

    case 'o':
      cout << "It is a vowel - o" << endl;
      break;

    case 'u':
      cout << "It is a vowel - u" << endl;
      break;

    default:
      cout << "It is a consonant" << endl;
      break;

    }
    return 0;
}

You can use this code in your C++ IDE. It will ask you to enter a character. This will return the type of alphabet entered – either vowel or consonant.

Please write comments below if you find anything incorrect, or you want to share more information about the topic discussed above. A gentle request to share this topic on your social media profile.

Author: Mithlesh Upadhyay

I hold an M.Tech degree in Artificial Intelligence (2023) from Delhi Technological University (DTU) and possess over 4 years of experience. I worked at GeeksforGeeks, leading teams and managing content, including GATE CS, Test Series, Placements, C, and C++. I've also contributed technical content to companies like MarsDev, Tutorialspoint, StudyTonight, TutorialCup, and Guru99. My skill set includes coding, Data Structures and Algorithms (DSA), and Object-Oriented Programming (OOPs). I'm proficient in C++, Python, JavaScript, HTML, CSS, Bootstrap, React.js, Node.js, MongoDB, Django, and Data Science.