Skip to main content
GameDev.net gamedev.net
🔒 Locked

quick switch statement question

Started by pavel989 Oct 16, 2008 at 11:07 PM 11 replies 1.3k views
Original Post
pavel989
pavel989
is a break; the only thing thatll stop it? can i do something like:

    switch (a)
    {
        case 1:
           return somefunc();
        case 2:
           return somefunc();

pavel989
pavel989
continue statement? thats new to me.

also, specifically, what would the somefunc() have to return? a break?
smitty1276
smitty1276
It would have to return whatever it is that the function returns. Return means that you are returning from the function... if the function returns type T, and you have "return foo()", then foo() is type T (or will cast) or it won't compile.
implicit
implicit
Quote:
Original post by pavel989
continue statement? thats new to me.
Technically continue can break out of a switch statement, thought they're not directly related. It'll simply continue the innermost loop, whether that be inside or outside of the switch (or even spanning case labels.)
I suppose any other old flow control statement can get you out of a switch as well, like a goto or a throw/longjmp.

At least that's the case in C/C++.

Quote:
also, specifically, what would the somefunc() have to return? a break?
Whatever the function the switch itself lies in returns. There's nothing special about a return inside of a switch, a return always returns from the function.
GenuineXP
GenuineXP
For example.

...std::string f1() {    ...}std::string f2() {    ...}enum {    foo,    baz};std::string baz(int value) {    switch (value) {    case foo:        return f1();    case bar:        return f2();    default:        break; // Could also just be a return.    }    return "what?";}...

The two functions f1 and f2 are used within the switch in the function baz. Because baz returns a std::string, the return value from f1 and f2 must be std::string or convertible to std::string, or the return statements wouldn't compile.

The default case could return instead of break, but I prefer breaking and then handling the default behavior outside of the switch.
pavel989
pavel989
well i know that, what im asking is if i can do "return somefunc();" to break after somefunc executes
EmrldDrgn
EmrldDrgn
Not to my knowledge, no. Although

somefunc();
break;

will do what you want, at least in C++. return somefunc(); would return from the function the switch is in, with the value returned from somefunc();. If the switch is in main, it will exit the program.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~I program in C++, on MSVC++ '05 Express, and on Windows. Most of my programs are also for windows.
GenuineXP
GenuineXP
Yes. The return statements within the switch function just like any other return within a function body. The function simply exits, and emits the specified value. This means the switch also terminates (i.e., breaks).

From the above example.

...std::string f1() {    return "hello";}...int main() {    int value = foo;    std::cout << baz(value) << "\n"; // Prints "hello".    std::cout << baz(-1) << "\n"; // -1 won't match a case, so this prints "what?".    return 0;}
pavel989
pavel989
ok, i think i get it. I will try this all out tomorrow. Thank you both.
DevFred
DevFred
Quote:
Original post by pavel989
what would the somefunc() have to return? a break?

You can't return breaks, only values of types.
pavel989
pavel989
yea i realized that when i actually thought about what i was asking. ty tho

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.