C++ Switch Statements

C++ provide multiple branch selection statement that is known as switch. A switch is a control statement in which the control of the program depends on the value passed to switch function. When a match is found, the statements associated with that constant are executed.

A case statement cannot exist by itself, outside a switch. In C++, the switch case statement is not a sequence of if-else statements and it is difficult to simulate the behavior of a switch case in this way. The switch case can be seen as an alternative to the if-else statement. Where you can conditionally choose between two statements.

Syntax :

switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}


The expression "switch (n)" is evaluated and its value is matched with the value of the constant specified in the case statement. If the match is found, the code associated with the case will be executed until the end of the break statement or switch statement. If n does not match any case then the default code is to be executed. The default statement is optional and no action occurs if it is missing.



Example :
#include 
int main()
{
int num = 1;
switch (num)
{
case 1: printf("Mynum is 1");
break;
case 2: printf("Mynum is 2");
break;
case 3: printf("Mynum is 3");
break;
default: printf("Mynum other than 1, 2 and 3");
break;
}
return 0;
}


Output :
Mynum is 1

Difference Between if-else and switch

Basis For Comparison If-Else Switch
Basic If statement is executed depends on the output of the expression inside the statement. The switch statement will be executed by the user.
Expression The if-else statement uses multiple statements for multiple choices. switch statement uses single expression for multiple choices.
Testing If-else statements test for logical expressions. Switch statements does not test for logical expressions.
Evaluation If statement evaluates for character, integer value, character, pointer or floating-point type or boolean type. switch statement evaluates for character or integer value.
Editing It is difficult to edit the if-else statement It is easy to edit the Switch statement
Sequence of Execution Either if statement will be executed or else statement will be executed. switch statement execute one case

Exercise:-

1. Can we use string value/variable in switch test condition?

A. Yes
B. No

View Answer


2. What will be output of the following code?

int num = 2;
switch (num + 2)
{
case 1: printf("Case 1: ");
case 2: printf("Case 2: ");
case 3: printf("Case 3: ");
default: printf("Default: ");
}

A. Case 1:
B. Case 2:
C. Case 3:
D. Default:

View Answer



Program :-

Program to built a simple calculator using switch Statement

#include <iostream>
using namespace std;
int main()
{
char o;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> o;
cout << "Enter two operands: ";
cin >> num1 >> num2;
switch (o) 
{
case '+':
cout << num1 << " + " << num2 << " = " << num1+num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1-num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1*num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1/num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! operator is not correct";
break;
}
return 0;
}


Output :

Enter an operator (+, -, *, /): +
-
Enter two operands: 2.3
4.5
2.3 - 4.5 = -2.2





Visit :


Discussion



* You must be logged in to add comment.