Is the following possible?

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.
Well considering that switch-case statements take a constant for the cases, I'd say no. You'd be better to use if statements ;)

For the multi-break, most languages do something like break(#); but this is not valid C++, it seems.
Last edited on
1) No.

2) No, that makes no sense really. How would you possibly know if a function is calling you or what their return value is? And escaping a function that needs to return a value is really odd too...

If you really want get out of a bunch of loops like that, use a goto. Although if you need to escape like 3+ loops you might want to look at your algorithm.
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?

Yes, that's exactly what exceptions do.
NO!

1) No.

Then why is that functionality available in other languages such as according to LB?
Because C++ doesn't have every feature that exists in every language ever.

Note this behavior can be mimiced as previously suggested (either with goto, or with *wince* exceptions).

C++ just doesn't have the break(3); syntax that some other languages do.
Well I thought that gotos were frowned upon because of their divergence from object oriented programming.

Seriously would it be that hard to implement in the next version of c++? It does seem to me to be rather useful.
Yeah, gotos are usually frowned upon for good reasons. But if you use it to jump out of a nested loop, just a few lines down, it's fine - there is not much you can do wrong and the code remains obvious and easy to read.
I considered using goto for such cases before, but eventually dismissed it. Just the fact of having to think of some arbitrary label name rubbed me the wrong way. Not that introducing an artificial flag is any better.

As to why it's not in C++... either too few people need it "loudly" enough or perhaps some misguided style ideals (heavens no, someone could misuse that feature. Imagine that...) were playing a role in this. Anyway, the upcoming C++ standard does not have it, so I wouldn't get my hopes up.
Not that the missing functionality is much of a hindrance in practice - for me, it's more a question of principle.
Last edited on
I've never needed it...like I said:

firedraco wrote:
if you need to escape like 3+ loops you might want to look at your algorithm.


Also,
wtf wrote:
I thought that gotos were frowned upon because of their divergence from object oriented programming


No, they are frowned upon because of the mess they can easily create if you aren't careful...they don't really have anything to do with OO.
Topic archived. No new replies allowed.