Switch Statement in C++
Definition: The switch statement is a control flow structure in C++ that allows for the execution of different blocks of code based on the value of a single variable or expression. This structure is more efficient and readable compared to using long chains of if-else if-else statements, particularly when comparing a variable to multiple constant values.
Basic Syntax:
- The syntax for the switch statement is as follows:
switch (expression) { case constant1: // code to execute if expression == constant1 break; case constant2: // code to execute if expression == constant2 break; ... default: // code to execute if expression doesn't match any case }Components:
- expression: Can be of integer, character, or enumeration type. It is the variable or expression being evaluated.
- case constant: Each case is followed by a constant value. If the expression's value matches this constant, the corresponding code block is executed.
- break: The break statement is crucial as it terminates the switch statement. Without it, execution will continue to subsequent cases (this is known as "fall-through") until a break is encountered.
- default: This is an optional case that acts as a catch-all. The code within this block is executed when none of the other cases match the expression's value.
Example of Switch Statement in C++
- Example Code 1:
#include <iostream>
using namespace std;
int main() {
int num = 5;
switch(num + 2) {
case 1:
cout << "Case1: Value is: " << num << endl;
case 2:
cout << "Case2: Value is: " << num << endl;
case 3:
cout << "Case3: Value is: " << num << endl;
default:
cout << "Default: Value is: " << num << endl;
}
return 0;
}
In this example, since
num + 2evaluates to 7, none of the cases (1, 2, or 3) will match, so the default case will execute, producing the output: "Default: Value is: 5". This exemplifies the importance of thebreakstatement, as without it, case fall-through can lead to unexpected outputs.- Example Code 2:
#include <iostream>
using namespace std;
int main() {
char op;
double num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> op;
cout << "Enter two numbers: ";
cin >> num1 >> num2;
switch (op) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
if (num2 != 0) {
cout << num1 << " / " << num2 << " = " << num1 / num2;
} else {
cout << "Error! Division by zero is not allowed.";
}
break;
default:
cout << "Invalid operator!";
}
cout << '\n';
return 0;
}
This example demonstrates a simple calculator using a switch statement that evaluates the operator entered by the user. It includes error handling for division by zero using an if statement within the switch.
- Example Code 3:
#include <iostream>
using namespace std;
int main() {
int dayNum;
cout << "Enter a number (1-7): ";
cin >> dayNum;
switch (dayNum) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
default:
cout << "Invalid number! Please enter a number from 1 to 7.";
}
cout << '\n';
return 0;
}
- Here, the user is prompted to enter a number corresponding to the days of the week. Depending on the input, the program outputs the respective day or an error message for invalid input.
Using Break Statement in a Loop
- Example Code 4: Break loop example
// break loop example
#include <iostream>
using namespace std;
int main() {
int n;
for (n = 10; n > 0; n--) {
cout << n << ", ";
if (n == 3) {
cout << "countdown aborted!";
break;
}
}
return 0;
}
- In this example, a countdown from 10 to 1 is displayed. When
nreaches 3, the string "countdown aborted!" is printed, and the loop terminates due to thebreakstatement. This showcases the ability of the break statement to exit from loops prematurely if certain conditions are met.