CPP/Switch
From ProgrammingExamples
Switch.cpp
#include <iostream> #include <string> int main(int argc, char *argv[]) { //switch with ints int test = 1; switch ( test ) { std::cout << "before cases" << std::endl; case 1 : std::cout << 1 << std::endl; break; case 2 : std::cout << 2 << std::endl; break; default : std::cout << "default" << std::endl; } //switch with chars char a = 'a'; switch (a) { std::cout << "before cases" << std::endl; case 'a' : std::cout << "yes" << std::endl; break; case 'b' : std::cout << "no" << std::endl; break; default : std::cout << "no" << std::endl; } /* //switch with strings (this is NOT supposed to compile) //the error is "switch quantity inot an integer" std::string testString = "test"; switch (testString) { std::cout << "before cases" << std::endl; case "test" : std::cout << "yes" << std::endl; break; case "something else" : std::cout << "no" << std::endl; break; default : std::cout << "no" << std::endl; } */ return 0; }