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.
Example:
#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 value of d is entered by the user.
If value of d is 1 then case 1 of switch statement gets executed i.e. statements written after case 1 are executed.
If value of d is 2 then case 2 of switch statement gets executed.
If value of d is 3 then case 3 of switch statement gets executed.
If value of d is 4 then case 4 of switch statement gets executed.
If value of d is 5 then case 5 of switch statement gets executed.
If value of d is 6 then case 6 of switch statement gets executed.
If value of d is 7 then case 7 of switch statement gets executed.
If value of d is other than above values then default case of switch statement gets 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. |