I'm still a beginner and maybe I don't see the global and real life use of switch statements, but I can't yet find a reason why a "case" statement doesn't break automatically when it drops through the bottom of its scope.
Is there a logical and practical reason to it? Do you sometimes need to pass from one case to another in the same switch statement? And wouldn't it be contratictory to the use of a switch statement if so?
(I'm talking of course about the case where a "case" drops through the bottom of its scope, not about breaking in the middle of it, in which case it is necessary)
Plus, you could use switch in a do/while loop, and it would continue looping until the user presses the key that escapes it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
do
{
cin >> state_var;
switch (state_var)
{
case 0: // do something
break;
case 1: // Do something else
break;
case 2: // Do something
break:
case 3:
cout << "Program terminated!!"<<endl;
break;
default: // If none of the expected numbers were pressed
cout << "Sorry, I don't understand what you wanted.." << endl;
}
}while(state_var !=3);