Difference between revisions of "CPP/Switch"
From ProgrammingExamples
< CPP
Daviddoria (Talk | contribs) (Created page with '==Switch.cpp== <source lang="cpp"> #include <iostream> using namespace std; int main(int argc, char *argv[]) { int test = 1; switch ( test ) { cout << "before cases" …') |
Daviddoria (Talk | contribs) (→Switch.cpp) |
||
Line 2: | Line 2: | ||
<source lang="cpp"> | <source lang="cpp"> | ||
#include <iostream> | #include <iostream> | ||
− | + | #include <string> | |
− | + | ||
− | + | ||
int main(int argc, char *argv[]) | int main(int argc, char *argv[]) | ||
{ | { | ||
+ | //switch with ints | ||
int test = 1; | int test = 1; | ||
switch ( test ) | switch ( test ) | ||
{ | { | ||
− | cout << "before cases" << endl; | + | std::cout << "before cases" << std::endl; |
case 1 : | case 1 : | ||
− | cout << 1 << endl; | + | std::cout << 1 << std::endl; |
break; | break; | ||
case 2 : | case 2 : | ||
− | cout << 2 << endl; | + | std::cout << 2 << std::endl; |
break; | break; | ||
default : | default : | ||
− | cout << "default" << endl; | + | 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; | return 0; | ||
} | } | ||
</source> | </source> |
Latest revision as of 14:38, 26 June 2010
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; }