Switch Statement
#include <iostream> using namespace std; /* Switch demo. */ int main() { int m; cin >> m; switch(m) { case 1: cout << "1" << endl; break; case 2: cout << "2" << endl; break; case 3: cout << "3" << endl; break; default: cout << "Otherwise" << endl; } }

The switch is also familiar from Java. A test expression is compared to each case expression, and the one that matches, if any, is executed. If none matches, and default is present, that is executed. Each case needs a break to end the case. C limits the expression and constants to integer types, while Java allows strings.