Technology Inside Out!

Index ¦ Archives ¦ Atom ¦ RSS

Conditional Statements : Switch Case

In the previous tutorial, we've discussed about if - else and nested if - else statements and how we can use them to test certain conditions and execute a block of code depending on whether the condition becomes true or false. In this tutorial, we'll be covering another Conditional Statement used in C known as Switch Case. The major difference between choosing to use if-else and switch is, when we have to compare a single variable to different set of values, then Switch is preferred, and if-else if preferred otherwise.

Conditional Statements : Switch Case C language

The basic semantics of a switch block statement is as follows :

switch(variable-name)
{
    case 1:
    .
    break;
    case 2:
    .
    break;
    case 3:
    .
    break;
    .
    .
    .
    .
    default:
    .
    break;
}

Here variable-name corresponds to the variable you want to compare with a set of values and inside the curly braces we have defined cases such that if value of variable is 1 then the statement 1 and statement 2 would execute, for variable having value 2, statement 3 and 4 would execute and so on. If none of the cases are true, then statement n corresponding to default would execute.

Let us take an example and understand how it's done. Let us consider we need to just print what number it is if the number is less than or equal to 5 else print number is not 1, 2, 3, 4 or 5. We'll consider that user has input a number which is stored in variable named num. This can be done through if - else statements as we studied earlier like this :

if(num == 1)
   cout<<"Number is 1 n";
else if(num == 2)
   cout<<"Number is 2 n";
else if(num == 3)
   cout<<"Number is 3 n";
else if(num == 4)
   cout<<"Number is 4 n";
else if(num == 5)
   cout<<"Number is 5 n";
else
   cout<<"Number is not 1, 2, 3, 4 or 5 n";

Now in the above case, since we need to compare just one variable with a set of values, this can be done easily through switch case as follows:

switch ( num )
{
   case 1 : cout<<"Number is 1 n";
   break;
   case 2 : cout<<"Number is 2 n";
   break;
   case 3 : cout<<"Number is 3 n";
   break;
   case 4 : cout<<"Number is 4 n";
   break;
   case 5 : cout<<"Number is 5 n";
   break;
   default : cout<<"Number is not 1, 2, 3, 4 or 5 n";
   break;
}

Notice here that I have mentioned break statement after each case , this is to terminate the the case above it if it's executed. Let's see if we didn't include break what would happen, so consider this code snippet.

#include<stdio.h>
int main()
{
  int num;
  cout<<"Enter a number ";
  cin>>num;
  switch ( num )
  {
     case 1 : cout<<"Number is 1 n";
     case 2 : cout<<"Number is 2 n";
     case 3 : cout<<"Number is 3 n";
     case 4 : cout<<"Number is 4 n";
     case 5 : cout<<"Number is 5 n";
     default : cout<<"Number is not 1, 2, 3, 4 or 5 n";
  }
return 0;
}

Now, if we run this and input 2 is given then output would be as shown below

Switch case in C language Output Screen

See, we not only got Number is 2 statement printed, but got all statements printed after that case, in a sequential manner, this is because in a switch condition we need to have conditional flow of statement using break statement.

Now, if you include break after each case but doesn't do it with default case, it would run fine too, this is because if the default case is true, then after executing set of statements with that case, the control would automatically be out of the switch block, so technically we do not need break after default case.

Now let's see how to match a character type variable. Consider a character (char) type variable named character_variable , and if we need to use switch, it can be done in this way:

switch ( character_variable )
{
   case 'a' : cout<<"Case a executed n";
     break;
   case 'b' : cout<<"Case b executed n";
     break;
   case 'c' : cout<<"Case c executed n";
     break;
   default : cout<<"This is default case n";
     break;
}

Here, notice that we have enclosed the character value between single quotes ( '   ' ).

Like wise, you can design your switch case for various purpose as/when you need to compare a single variable value with a set of values.

In next tutorials we'll know about the Storage Classes in C. If you have any problem with anything till now, feel free to let us know through comments section below, we'll be glad to help you.

Keep Coding !

© The Geeky Way. Built using Pelican. Theme by Giulio Fidente on github.

Disclaimer Privacy policy