switch statement in C++ | Working of switch statement in C++

switch statement in C++ | Working of switch statement in C++

It is a selection statement that works with a variable or expression of short, long, int or char data type whose value is compared with a list of values known as cases.

If  value of expression/variable matches with any of the case value, the block of statements associated with that case gets executed.

Syntax of switch statement:

switch(Expression)
{
case val1:
                        Block1;
case val2:
                        Block2;
case val3:
                        Block3;
:
:
default:
                        BlockN;     
}

switch keyword specifies the beginning of switch statement.

Expression is any integer or character variable or expression.

case is a keyword to specify cases within switch statement

val1,val2,val3 are constant values/expressions that are compared with expression or variable. 

If the value of Expression matches with val1, Block1  gets executed.

If value of Expression matches with val2, Block2 will execute

If the value of Expression matches with val3, Block3 will execute and so on.

If value of expression doesn’t match with any of the case values, default case i.e. BlockN will execute.

break is written after each case  so that program control should jump out of switch statement after execution of statements within a case. 

 

Program to demonstrate the use of switch statement in C++.

#include<iostream>
using namespace std;
int main()
{
 int d;
 cout<<“\nEnter a value between 1 and 7=";
 cin>>d;
 switch(d)
 {
  case 1:
      cout<<“Sunday";
      break;
  case 2:
      cout<<“Monday";
      break;
  case 3:
      cout<<“Tuesday";
      break;
  case 4:
      cout<<“Wednesday";
      break;
  case 5:
      cout<<“Thursday";
      break;
  case 6:
      cout<<“Friday";
      break;
  case 7:
      cout<<“Saturday";
      break;
 default:
      cout<<“Wrong choice";
      break;
 }
 return(0);  
}
Output
Enter a value between 1 and 7:-2 Monday
Description
In the above program, variable d has been entered  2. Value of d is compared with all case values within the switch(day) statement. As value of day matches with case 7:  So the statement printf(“Saturday”) would be executed.



DIFFERENCE BETWEEN IF AND SWITCH STATEMENT IN C

IF STATEMENT SWITCH STATEMENT
1. If statement works with  a relational or logical expression. 1. It works with an integer or character expression or variable .
2. If statement is categorized into four types 2. It has no types.
3. There can be multiple Conditions in if statement. 3. There can be multiple cases
4. If statement can work with any relational or logical operator. 4. It can only work with equality operator.
5. Syntax of if statement:
if(Condition)
{
Statement-Block;
}
Statement-X;
5. Syntax of switch statement:
switch(Expression)
{
case value1:
Block1;
case value2:
Block1;
case value3:
Block1;
:
:
:
default:
BlockN;
}
8. There are no cases or case values in if statement 8. There are various cases in switch statement and all case values must be constant values.

Spread the love
Lesson tags: DIFFERENCE BETWEEN IF AND SWITCH STATEMENT IN C, switch statement in c, Working of switch statement in C++
Back to: C++ Programming Notes
Spread the love