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.