Hey there! Welcome to the third part of my series, C++: Explain like I’m five. In the previous part, we covered operators, user input, and if/else statements. If you haven't read that one, you can do that here. Please go through this series in the pre-defined sequence or if you are a beginner, then you may not be able to understand some of the concepts that I've explained.
Today we are going to discuss switch cases. So, without any further ado, let's dive in!
Switch Case
The switch case statement is generally used when there is more than one condition and we need to perform different actions based on the given conditions. The lengthy if.....else-if or switch cases are used when we have multiple conditions and we need to execute a block of statements when a particular condition is satisfied. But the issue with lengthy if..else-if is that the code becomes complex when there are several conditions. Whereas, the switch case is a much more efficient and clean method of handling such scenarios.
Syntax:
switch (variable or an integer expression)
{
case constant1:
//Code block to be executed if the expression is equal to constant 1
;
case constant2:
//Code block to be executed if the expression is equal to constant 2
;
default:
/* Code block to be executed if the expression does not
match any of the given constants */
;
}
Usually, a break statement accompanies a switch case though its use is optional but recommended in certain conditions. The default keyword is optional too. We will look at "break" in detail later in the series too.
Example:
#include <iostream>
using namespace std;
int main() {
int day = 6;
switch (day) {
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;
}
return 0;
}
Output:
Saturday
The default keyword
It specifies some code to run if there is no case match. Let's tweak the above given example a bit.
Example:
#include <iostream>
using namespace std;
int main() {
int day = 8;
switch (day) {
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: //Our little tweak
cout << "Looking forward to the weekend!";
}
return 0;
}
Output:
Looking forward to the weekend!
See what I did there? There are 7 cases in our code. But the input was something that did not match with case constants, so instead of crashing, the compiler goes to the default statement and executes its code which was "Looking forward to the weekend!"
Why use break statement in the switch cases?
Before discussing anything, let's take a look at an example of a code without the break statement.
Example:
#include <iostream>
using namespace std;
int main(){
int a=3;
switch(a) {
case 1:
cout<<"Case1 "<<endl;
case 2:
cout<<"Case2 "<<endl;
case 3:
cout<<"Case3 "<<endl;
case 4:
cout<<"Case4 "<<endl;
case 5:
cout<<"Case5 "<<endl;
default:
cout<<"Hey "<<endl;
}
return 0;
}
Output:
Case3
Case4
Case5
Hey
In the program above, we have the variable "a" inside the switch braces, which means whatever the value of variable "a" is, the corresponding case block gets executed. We have passed integer value 3 to the switch, so the control switched to case 3. But, as we don’t have a break statement after case 3, that resulted in the flow continuing to the subsequent cases till the end. However, this is not what we wanted. We merely wanted to execute the right case block and ignore the other blocks. We can get our desired result by using a break statement after every code block. Break statements are used only when you want the flow of your program to come out of the switch body.
As you guys now understand the importance of using the break statement so let's take a look at the same program but with break statements.
#include <iostream>
using namespace std;
int main(){
int a=3;
switch(a) {
case 1:
cout<<"Case1 "<<endl;
break;
case 2:
cout<<"Case2 "<<endl;
break;
case 3:
cout<<"Case3 "<<endl;
break;
case 4:
cout<<"Case4 "<<endl;
break;
case 5:
cout<<"Case5 "<<endl;
break;
default:
cout<<"Hey "<<endl;
}
return 0;
}
Output:
Case3
See? After using the break statement, we got our desired output!
Now that you guys understand the switch statements, let's do a fun little activity ;) We will build a simple calculator using the switch and break statements.
Calculator
#include<iostream>
using namespace std;
int main()
{
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper)
{
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! The operator is not correct";
break;
}
return 0;
}
Explanation of Code:
• First, we initialized the variable for user input, that is "oper".
• Then we initialized the operands, num1 and num2. We use float as by doing so, we can also work with such numbers like 13.5, 2.1, etc.
• Then we use cin again so that the user can enter the numbers on whom the operator will work upon.
• Then code block corresponding to the operator entered would be executed.
• If the operator entered does not match the pre-defined case constants, then the statement of default keyword would be printed.
As for the output of this code, it would be much better if you guys experiment around with it a bit. That way you can deepen your understanding of the switch case statements.
I hope you guys did learn something. Well, that's all for today. Wouldn't want to burden you guys with more concepts right now ;) In the next part, we will discuss loops so stay tuned!