I was just fooling around and was pondering some of these such as:
Can a switch statement do either of the following:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
switch (x)
{
for (int i = 1; i <= 10; ++i)
{
case i:
yada(i);
break;
}
}
// or
switch(y)
{
case y % 2 == 0:
etc();
case y % 3 == 0;
so_on_and_so_forth();
}
|
In the latter example, it is testing whether if y is even. That way, without using a break; it would be possible to test for multiple conditions, such as if a number is both even & divisible by 3, such as 6.
In the former example I realize that the break statement would only actually break the current for loop, not the switch statement it self so that posed the question:
Is there a functionality in c++ that allows to break multiple control structures in a single line of code:
such as something like:
break_2;
although I don't necessarily know what it would be called.
And similarily that question got me to thinking is there a similar functionality of that of the break command as applicable to functions. Would it be possible to break (without returning its return type) off from execution of a funcition? Or would it be possible to quote-un-quote "return a return", so that a calling function would be forced to return or break if the called function executed such a command?
i suppose you could just use a sentinle value such as
if (function1() == -99) return r_v;
, but that simply isn't very elegent or efficient. Plus you would be unable to return a string, say if you wanted to.