Switch statement vs if else

They seem like they are the same and can both be used for the same program so my question is is there any situation were you may need to use one over the other or does it not matter?
I've never really seen a huge difference but then again I've always just used if else statements. I'm a novice though as well. Maybe a pro can come in here and clear some stuff up for us :P
If you have a menu or something similar where the user has to select a key; a for something, b for another, etc. a switch will almost always be better. A switch is limited in that it can only take an int or char as an argument though so you cannot use strings or other arguments as the argument for example.
case num < 10: is not valid while case 10: is.
Generally you would use switch if there are more than two cases, and you're checking the value of one variable. If-else in most other situations. A lot of people think switch is bad practice though, since you could omit a break statement and mess up the whole purpose.
--There is no significant execution / performance difference between if-else and switch. The difference observed is due to the sample space chosen.
-- If using if-else, its always recommended to put frequently used if condition at the top of if-else ladder

Nevertheless, i prefer switch for its readability.
Sometimes it's not possible to use switch-case. This is ok:
1
2
3
4
5
6
7
8
9
    char input;
    cin >> input;
    
    switch (input)
    {
        case 'a':
            cout << "one" << endl;
            break;     
    }

However, change the type of input from char to string like this and it will not compile:
1
2
3
4
5
6
7
8
9
    std::string input;
    cin >> input;
    
    switch (input)
    {
        case "a":
            cout << "one" << endl;
            break;    
    }
because the quantity used the the switch must be an integer. Also it is not possible to test several different variables in a single switch statement, which indeed is possible in some other languages.

The switch-case in C++ is quite limited. It does one thing, it does it reasonable well, I tend to use it where it will add to the legibility, though that may sometimes be a matter of preference.
the difference between it, is the switch statement cannot compare a string unlike if statement
comparison:
if statement- string,char,int,bool,double
switch statement - char,int,bool,double
if statement
1
2
3
4
5
6
string a;
cin>>a;
if(a=="hello")
{
cout<<"nice";
}

switch statement
1
2
3
4
5
6
string a;
cin>>a;
switch (a)
{
case "hello": cout<< "nice" ; break; // error
}

Last edited on
I don't know of anyone who recommends switch as bad practice.

The switch statement is a specialization of a common case if..else chain: comparing a single ordinal value against a finite list of values. Many CPUs provide instructions to do just this (string search instructions), which translates into a convenient jump table.

Modern compilers can recognize this optimization in an equivalent if..else chain so these days the code produced is equivalent.

The automatic drop-through is ridiculed (and rightly so), but unless you are a language designer it really makes no difference.
Topic archived. No new replies allowed.