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.

1
1

C++ 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 code

1. 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.

php Copy code

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.

php Copy 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.

vbnet Copy code

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.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners