C++ switch() case control execution statement

The switch() statement is like the ‘if()’ else statement but the syntax is a bit different.It uses ‘switch()’ and ‘case’ statement to control the flow of the execution of the code.The pictorial representation of the switch() case is given below.

The syntax of ‘switch() case’ is given below.

switch(exp1)
{
  case expa :
    //code 1
    break ;

  case expb :
    //code 2
    break;

  case expc :
    //code 3
    break;

  default :
    //code 4
} 

If exp1 matches with expa then code 1 is executed or if exp1 matches with expb then code 2 is executed and so on.If exp1 does not match with expa or expb or expc then the code under default i.e. code 4 is executed.

Note the expressions ‘expa’,’expb’ and ‘expc’ cannot be variable.It can only be integer values or characters or bool(true or false),it cannot be anything else if you use anything else it is an error.

Note the ‘break;‘ statement after every code.The function of this statement is to stop iterating the code under the switch() statement.

A program using the switch() statement is given below.

char c=’d’;

switch(c)
{
case ‘a’ :  //If c is equal to ‘a’ case
    cout<< “The character in c is a “;
    break;

case ‘b’ :  //If c is equal to ‘b’ case
    cout<< “The character in c is b “;
    break;

default:  //if c is unknown case
    cout<<“The character of c is unknown “;
    break ;
}

The above program is quite simple,the program takes the ‘c’ variable in the ‘switch’ and compare the value of ‘c’ value which is the character ‘d’,with each of the character following the ‘case’ statement.As we can see there is no character ‘d’ in any of the case so the code under ‘default’ statement is printed out.Hence the output is:

The character of c is unknown
 

The argument passed to switch cannot be string or floating point type

The expression or an argument pass to switch() statement cannot be a string or a floating point or ,it can only be a character or an integer,if you pass a string or a float you will get an error.

string s=”happy” ;

switch( s ) //You will get error here
{
 case 89.75 : //error
    cout<< “new” << endl ;
    break;

  default:
    cout<< s << endl;
    break ;
}

double pi=3.14 ;

switch ( pi ) //error!!
{
case 3.145: //error can only be integer or character or bool
 cout<< pi ;
 break;
 
case 4: //work fine
 cout<< pi ;
 break;

default:
 break;
}

But if cast the switch expression to int of char type the program will work fine.

float f=90.34;

switch( (int)f ) //work fine
{
case 90 :
 cout<< f ;
 break;

case 50:
 cout<< 50;
 break;

default:
 break;
} 

The output,

90.34

Note if you want to compare string or use string as the expression for decision making then you can use ‘if() else’ statement.

Link :C++ if() else


Which is faster if() else or switch() statement?

The ‘if() else’ and ‘switch() case’ statement are pretty much similar and when the time comes for which you should choose to use in your program you will consider whichever is faster.So there is certain curiosity as to which control statement is faster if() else or switch() statement.

To test it I wrote a program with 5000 cases each for if() else and switch() statement.The program will accept an integer value and the time taken by the if() else or switch() statement to find the correct case/value is noted.The program is given below.

i)The first program uses if() else statement.
ii)The second program uses switch() statement.
 
***Note:The programs are tested in Ubuntu.
 

 
 /****If() else case ****/

#include <iostream>
#include <sys/time.h>

using namespace std ;

typedef unsigned long long timestamp_t;

static timestamp_t get_timestamp ()
{
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}

int main()
{
int i ;
cout<<“Enter any number between 0 to 1000 \n”;
cin>> i ;
cin.ignore() ;

timestamp_t t0 = get_timestamp() ;

if(i==0)
{
  cout<< i << endl ;
}
else if(i==1)
{
  cout << i << endl ;
}
else if(i==2)
{
  cout << i << endl ;
}
.   /*else if from 3 to 4998 is not shown here */
.
.
else if(i==4999)
{
  cout << i << endl ;
}
else if(i==5000)
{
  cout<< i << endl ;
}

timestamp_t t1 = get_timestamp();
double secs = (t1 – t0) / 1000000.0L;

cout<< secs ;

cin.get() ;
return 0 ;
} 

The complete source code program is given below.


 

**switch() ***/

#include <iostream>
#include <sys/time.h>

using namespace std ;

typedef unsigned long long timestamp_t;

static timestamp_t get_timestamp ( )
{
struct timeval now;
gettimeofday (&now, NULL) ;
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}

int main( )
{
int i ;
cout<<“Enter any number between 0 to 200 \n”;
cin>> i ;
cin.ignore();
timestamp_t t0 = get_timestamp() ;

switch(i)
{
 case 0:
  cout<< i << endl ;
  break ;

 case 1:
  cout<< i << endl ;
  break ;
 . /*Switch case from 2 to 4998 is not shown here */
 .
 .
 case 4999:
  cout<< i << endl ;
  break ;

 default :
  cout<< i << endl ;
  break ;
}

timestamp_t t1 = get_timestamp();
double secs = (t1 – t0) / 1000000.0L;

cout<< secs ;
cin.ignore();
return 0;
}

The table below shows the input value and the time taken by the if() else and switch() statement to find the correct code of the input value.

Input value if() else switch()
2000 4.2e-05 3.4e-05
777 3.6e-05 5.6e-05
4500 0.000144 3e-05
3999 5.3e-05 3.1e-05
4555 8.4e-05 3.3e-05
1 3e-05 5e-05
2890 3.2e-05 2.9e-05
Average 6.0142e-05 3.757e-05

You can see that the average time of ‘switch()’ is lesser than the ‘if() else’ statement.If you are running the program to check the execution time of the if else and switch() statement you may not get the same execution time for the same input as given in the table above but the average value of switch() will always be lesser than the if() else statement.




Leave a Reply

Your email address will not be published. Required fields are marked *