Switch - Case statements in C++ - Algomentor

Switch - Case statements in C++


Switch case statements in c++

Hi, friends welcome to the new series of data structure and algorithm made easy in this particular article you will know about the switch-case in c++ language all of you basic concept will be clear after reading this article about switch-case in c++.

What is the switch-case statement in C++?


The expression of switch-case is similar to if-else or conditional statements in if-else we check using condition is true/false. in switch-case we give input to the switch and according and its case will be matched with all these cases. later in this section, we will do some code implementation for the same.

Switch-case of if-else which is better?

To be honest both have their own advantage and disadvantages it's only up to you how can you deal with it. if you have 2-3 condition then you should go with the if-else statement but if you have a large number of cases then you should go with a switch-case. this gives you clarity of each case and its easy to debug if it has any issue in the code we directly jump to the case. Another major thing is that it does not necessary that you find your condition in if-else you may skip some conditions but in switch-case, in the end, you have a default case.

one better use of if-else is that when you have a range like if a number is between 1 to 100 then do these things. but in switch case, you can't do that to do that you need to define all 100 cases to do the work. so you need 100 lines of code to the work that work can be done 3 lines using if-else statements that totally up to your situation what you need to use.

How is a typical program of switch-case look likes?

switch(n) // suppose n is natural number
{
    case 1:  // when n = 1 this case exicuted
          break;
    case 2: // when n = 2 this case exicuted
          break;
    case 3: // when n = 3 this case exicuted
          break;
    default: // when n = 4 to n = ∞ this case exicuted
          break;
}

Is switch-case is faster then if-else?

In some cases, the answer is yes but if-else is a more practical approach because many times the conditions are like when n=1 to n=100 to this thing and when n = 101 to n=500 do that thing so for these all coder prefer to use if-else. but still, the concept of switch-case is also important so we need to learn them also.

Write the code to use a switch case statement when the user gives 2 it prints "The first prime number" if the user gives 3 it prints "The second prime number". if any other number is given it should print "I don't know".

#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
switch(n)
{
case 2:
cout<<"The first prime number";
break;
case 3:
cout<<"The second prime number";
break;
default:
cout<<"I don't know";
}
}


Write the same code of the above program with an if-else statement.

#include<iostream>
using namespace std;

int main()
{
int n;
cout<<"Enter a number ";
cin>>n;
if(n==2)
{
cout<<"The first prime number";
}
else if(n==2)
{
cout<<"The second prime number";
}
else
{
cout<<"I don't know";
}
}

// hear is the output for the code (both have the same input and same output)

Switch case statements in c++


I hope you like the concept of switch-case any you query regarding switch-case and if-else will be answered in this program and if you end up like reading this article then please leave your lovely comment below and share this article with your friends in the social media.

Thank you so much for reading we appreciate your effort toward learning data structure and algorithm 😃.

Hi if you want to write a post on our website then please check this article on How to do a guest post.

Post a Comment

0 Comments