SEARCH THIS BLOG

switch case IN C/C++

The switch case is a programming construct in C/C++ which allows us to match the result of an expression to certain cases and then display the matching statement. Let us read in details about switch case.

The switch() Statement in C/C++ :

C++ provides a multiple-branch selection statement known as switch. This selection statement successively tests the value of an expression against a list of integer or character constants. When a match is found, the statements associated with that constant are executed.

The general syntax of switch statement :

switch (exp)

{

case constant 1 : statement sequence 1; break;

case constant 2 : statement sequence 2; break;

case constant 3 : statement sequence 3; break;

……….

case constant n-1 : statement sequence n-1; break;

default : statement sequence n;

}

The exp is evaluated and its value is matched against the values of the constants specified in the case statements. When a match is found, the statement sequence associated with that case starts executing. The control goes on executing all the following sequences till it encounters break or it reaches the end of switch.

If a case statement does not include a break statement, then the control continues right on the next case statements until a break is encountered or the end of switch is reached. This situation (missing break in case statements) is called fall through.

The default case is executed when no match is found. The default statement is optional, and if it is not present, the control simply comes out of the switch block if none of the cases match.

Whenever a break statement is encountered in a switch statement, program execution jumps out of the switch block to the line immediately after switch.

The following flowchart represents basic working of a switch statement.

switch case in C/C++
 

Points to keep in mind while using switch case :

First, the expression provided in the switch should result in character or integer type constant. Otherwise, the switch statement becomes invalid and compiler shows error.

Example :

Valid expressions – switch(1+3*6)

                                     switch(1*2/9%3)

variables can also be used but they should be storing some integer or character value      switch(a+b*c)

 

Second, two cases cannot have the same constant, i.e. duplicate cases are not allowed.

 

Third, the default statement is optional and is executed when none of the cases match the value of the expression.

 

Fourth, break is an important keyword in switch case. It is optional but should be carefully used in order to avoid fall through.

 

Fifth, Nesting of switch case is allowed, i.e. there may be one or more switch cases inside the parent switch block. But it is generally avoided as it makes the program complex and reduces readability.

 

Sixth, all case constants must be either character type or integer type. Also, C++ allows constant variables (declared using const) as case constants but C doesn’t. So, for switch statement in C, all case constants must be literals and not associated with any variable. For instance, the code

const int a=1;const int b=2;

switch(1)

{

  case a : cout<< 1 ;break;

  case b : cout<< 2 ;break;

  default : cout<< “more than 2”;

}

works fine with C++ compiler but shows error in C.

 

Here is a sample program to find greater of two numbers using switch case :

#include<iostream>

using namespace std;

int main()

{

int a,b; cin>>a>>b;

switch ((int)(a>b))

{

     case 0: cout<<b<< “ is greater ”;

                  break;

     case 1 : cout<<a<< “is greater ”;

}

return 0;

}

Input : 60 68

Output : 68 is greater

 

An interesting exercise for you is to write the following programs using if-else and then using switch.

1. Check if a character is vowel or not. Take input from user.

2. Print the day of the week depending on the number input by user. Assume the      number to be between 1 to 7 with 1 being Monday and 7 being Sunday.

Try making a simple calculator using switch case.

I am not including solutions so that you may practice it yourself. If you want to share your code or ask any doubt you can comment on this post or you can mail me at coding.nkcoder@gmail.com.

 

 

 

Which topics you want an article on? Comment down on this post.

Do not stop here. Read all articles related to C++ here.

If you want to contribute any article for the website, feel free to share it on coding.nkcoder@gmail.com. We will publish it on the website with your name.

Download the pdf form of this article here.

 

 

 


Post a Comment