Hi there, we’re Harisystems
"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".
For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.
1C++ Switch-case Statement
In C++, the switch-case statement provides a convenient way to execute different blocks of code based on the value of a variable or an expression. It allows you to compare a single value against multiple cases and perform different actions accordingly. In this article, we will explore the usage of the switch-case statement in C++ with examples.
less Copy code1. Basic switch-case Statement
The most basic form of a switch-case statement consists of the switch keyword followed by a variable or an expression enclosed in parentheses. Each case represents a specific value or a range of values that will be compared against the switch variable. Here's an example:
#include <iostream>
int main() {
int day = 3;
Copy code
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
break;
case 2:
std::cout << "Tuesday" << std::endl;
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
case 4:
std::cout << "Thursday" << std::endl;
break;
case 5:
std::cout << "Friday" << std::endl;
break;
default:
std::cout << "Invalid day" << std::endl;
break;
}
return 0;
}
In the above code, we use a switch-case statement to check the value of the variable day
. Depending on its value, the corresponding case is executed. If none of the cases match, the default case is executed.
2. Multiple Cases
In C++, you can group multiple cases together to execute the same block of code for different values. Here's an example:
#include <iostream>
int main() {
char grade = 'B';
c
Copy code
switch (grade) {
case 'A':
case 'a':
std::cout << "Excellent" << std::endl;
break;
case 'B':
case 'b':
std::cout << "Good" << std::endl;
break;
case 'C':
case 'c':
std::cout << "Average" << std::endl;
break;
default:
std::cout << "Invalid grade" << std::endl;
break;
}
return 0;
}
In the above code, we use the switch-case statement to check the value of the variable grade
. We group the cases for both uppercase and lowercase grades together to execute the same block of code.
3. Fallthrough
By default, after executing a case, the control will exit the switch block. However, sometimes you may want to execute multiple cases consecutively. In such cases, you can use the fallthrough behavior by omitting the break
statement. Here's an example:
#include <iostream>
int main() {
int num = 3;
c
Copy code
switch (num) {
case 1:
std::cout << "One" << std::endl;
// Fallthrough
case 2:
std::cout << "Two" << std::endl;
// Fallthrough
case 3:
std::cout << "Three" << std::endl;
break;
default:
std::cout << "Invalid number" << std::endl;
break;
}
return 0;
}
In the above code, when the variable num
is 3, the cases for both 2 and 3 will be executed. This behavior is achieved by omitting the break
statement after the case for 2.
The switch-case statement provides a flexible way to handle multiple conditions based on the value of a variable or an expression. It is particularly useful when you have a fixed set of possible values to compare against. Practice using switch-case statements with different scenarios, including fallthrough behavior and multiple cases, to effectively control the flow of your C++ programs.