Use app×
QUIZARD
QUIZARD
JEE MAIN 2026 Crash Course
NEET 2026 Crash Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
515 views
in C++ by (178k points)
Can I use the switch statement for non-integer types?

Please log in or register to answer this question.

1 Answer

0 votes
by (178k points)

Starting from C++17, you can use the switch statement with non-integer types, such as characters and enumerated types.

Here's an example code snippet to demonstrate the usage of the switch statement:

#include <iostream>

int main()
{
    int choice;
    std::cout << "Enter a number (1-3): ";
    std::cin >> choice;

    switch (choice)
    {
        case 1:
            std::cout << "You chose option 1.\n";
            break;
        case 2:
            std::cout << "You chose option 2.\n";
            break;
        case 3:
            std::cout << "You chose option 3.\n";
            break;
        default:
            std::cout << "Invalid choice.\n";
            break;
    }

    return 0;
}
 

In this example, the user is prompted to enter a number. The switch statement checks the value of choice and executes the corresponding code block based on the entered value. If an invalid choice is entered, the default case is executed.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked May 31, 2023 in C++ by kvdevika (178k points)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...