Why choose switch case in C++

Last Updated :

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.

Comment
Next Article
Mithlesh Upadhyay Published 16 Dec, 2022 · 3 min read

Mithlesh Upadhyay is a Computer Science and AI expert from Madhya Pradesh with strong academic background (BE in CSE and M.Tech in AI) and over six years of experience in technical content development. He has contributed tech articles, led teams, and worked in Full Stack Development and Data Science. He founded the w3colleges.org portal for learning resources.

Similar Reads

  • Four Pillars of Object-Oriented Programming (OOPS)

    Get an overview of four pillars of object-oriented programming (OOP) and see their suitable real life examples. What is OOP? Picture a cluttered garage🛠️. Tools are scattered everywhere,…

    4 min read
  • Find the Nth Triacontakaihenagonal Number

    Given a number N, the task is to find Nth triacontakaihenagonal number. A triacontakaihenagonal number is class of figurate number. It has 31 – sided polygon called triacontakaihenagon.…

    1 min read
  • Check if a Number is a Tetracontagon Number

    Given an integer N, the task is to check if it is a Tetracontagon number or not. Tetracontagon number is class of figurate number. It has 40 –…

    2 min read
  • Find max rational number in given Array

    Given the Numerator and Denominator of N Rational Numbers, the task is to find the maximum Rational Number from the array. Examples: Input: N = 2 num[] =…

    2 min read
  • Check if a Number is Pentacontagon

    Given an integer N, the task is to check if it is a pentacontagon number or not. Pentacontagon number is class of figurate number. It has 50 –…

    2 min read
  • Check if a Number is Triacontakaidigon

    Given an integer N, the task is to check if it is a Triacontakaidigon number or not. Triacontakaidigon number is class of figurate number. It has 32 –…

    2 min read