Thread: Quick question concerning switch statements

  1. #1
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43

    Quick question concerning switch statements

    This code won't work, I'm assuming it's because the case can't be a logical statement.

    The actual program I'm working on would use a much more complex switch than this, this is simply a small example of the concept.

    Code:
    #include <iostream>
    using namespace std;
    
    int main( )
    {
        int x = 1;
        switch (x)
        {
        case (0 || 1):
            cout << "X is either 0 or 1" << endl;
            break;
        case (1 || 2):
            cout << "X is either 1 or 2" << endl;
            break;
        case (2 || 3):
            cout << "X is either 2 or 3" << endl;
            break;
        }
        system("pause");
        return 0;
    }
    My question is, what's the easiest way to accomplish similar results?

    Ideally, I'd want the output generated to be

    X is either 0 or 1
    X is either 1 or 2

    Thanks
    Last edited by Shaun32887; 08-01-2008 at 04:09 PM.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Switches have to be constant...so you could just use a series of ifs
    Last edited by prog-bman; 08-01-2008 at 04:12 PM.
    Woop?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Quote Originally Posted by prog-bman View Post
    Switches have to be constant...so you could just use a series of ifs
    There's no way around that?

    I chose switches because for what I'm doing, the if statements will be MUCH slower... and there's a lot of data.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    You can use the fall-through property of switches:

    Code:
    switch (x) {
        case 0:
        case 1:
            cout << "X is either 0 or 1" << endl;
            break;
    From there on, since 1 has already been handled, in the next hypothetical case "1 or 2" the value would never be 1 - it can only be 2.

    However, there are certainly possibilities to write the above program without a switch or an if-else chain (keeping the messages somewhat broken as in the original code).

    Code:
    int main()
    {
        std::string messages[] = {"X is either 0 or 1" , "X is either 0 or 1" , "X is either 1 or 2", "X is either 2 or 3"};
        int x = 1;
    
    //a range check should go here: the default case
        std::cout << messages[x] << std::endl;
    }
    Polymorphism is another means of replacing switches.
    Last edited by anon; 08-01-2008 at 04:30 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Shaun32887 View Post
    There's no way around that?

    I chose switches because for what I'm doing, the if statements will be MUCH slower... and there's a lot of data.
    No, there is no way around the way the language is defined. They must be constant values.

    Your understanding of switches and if statements and their speed seems to be a bit lacking. A switch statement is only beneficial in terms of speed of execution because of this fact that the compiler knows exactly where to jump to in the generated machine code based upon a single number in a single variable. A series of if statements could theoretically be compiled to be just as fast if they are written in the same manner.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Duplicate post, see bottom reply.
    Last edited by Shaun32887; 08-01-2008 at 04:31 PM.

  7. #7
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Quote Originally Posted by MacGyver View Post
    No, there is no way around the way the language is defined. They must be constant values.
    I didn't mean I was trying to get around the way the language was defined. I was simply asking the more experienced programmers if there was a quick way that would produce the same results. The switch statement suggestion was similar to what I was looking for, but as I said, it would take a long time because it would have to be executed more times in the loop.


    Quote Originally Posted by MacGyver View Post
    Your understanding of switches and if statements and their speed seems to be a bit lacking.
    It's not how fast the if statement executes versus how fast the switch statement executes, it's the number of times the statements will have to be run. With the switch statement, I can get it all done in 1 pass and the computer would only be making three decisions per input.

    Then again I am pretty tired and I might be able to make it work. Hmm...
    Last edited by Shaun32887; 08-01-2008 at 04:32 PM.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Quote Originally Posted by anon View Post
    You can use the fall-through property of switches:

    Code:
    switch (x) {
        case 0:
        case 1:
            cout << "X is either 0 or 1" << endl;
            break;
    Well, while it would have to fall through exactly 1 case, I'd have to set up some kind of flag and then if then break thing. That actually could work, I'll look into this.

    Quote Originally Posted by anon View Post
    However, there are certainly possibilities to write the above program without a switch or an if-else chain (keeping the messages somewhat broken as in the original code).

    Code:
    int main()
    {
        std::string messages[] = {"X is either 0 or 1" , "X is either 0 or 1" , "X is either 1 or 2", "X is either 2 or 3";
        int x = 1;
    
    //a range check should go here: the default case
        std::cout << messages[x] << std::endl;
    }
    Polymorphism is another means of replacing switches.


    Sorry, I didn't see all of this before.

    My crash course on c++ skipped polymorphism, I'll check it out.


    This is exactly what I was looking for, thanks for the help.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    With the switch statement, I can get it all done in 1 pass.
    I don't understand that. You should be able to turn any switch statement into an if-else chain (well, fallthroughs might need goto, unless you can combine the cases with boolean operators).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #10
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Quote Originally Posted by anon View Post
    I don't understand that. You should be able to turn any switch statement into an if-else chain (well, fallthroughs might need goto, unless you can combine the cases with boolean operators).
    Yeah, you're right. I was thinking of another less efficient way to write the if statements.



    Still, I would have the same problem.


    Code:
    #include <iostream>
    using namespace std;
    
    int main( )
    {
        int x = 1;
    
        if (x == 0 ||x == 1)
            cout << "X is either 0 or 1" << endl;
        else if (x == 1 ||x == 2)
            cout << "X is either 1 or 2" << endl;
        else if (x == 2 ||x == 3)
            cout << "X is either 2 or 3" << endl;
        system("pause");
        return 0;
    }
    This would still only display

    "X is either 0 or 1"

    Since it won't execute ALL the satisfied branches, just the 1st satisfied branch.


    I think the conditional fallthrough might be my solution.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Shaun32887 View Post
    This code won't work, I'm assuming it's because the case can't be a logical statement.

    The actual program I'm working on would use a much more complex switch than this, this is simply a small example of the concept.

    Code:
    #include <iostream>
    using namespace std;
    
    int main( )
    {
        int x = 1;
        switch (x)
        {
        case (0 || 1):
            cout << "X is either 0 or 1" << endl;
            break;
        case (1 || 2):
            cout << "X is either 1 or 2" << endl;
            break;
        case (2 || 3):
            cout << "X is either 2 or 3" << endl;
            break;
        }
        system("pause");
        return 0;
    }
    My question is, what's the easiest way to accomplish similar results?

    Ideally, I'd want the output generated to be

    X is either 0 or 1
    X is either 1 or 2

    Thanks
    Why does each case have 1 test that is the same as another case? Shouldn't it be more like:
    Code:
        switch (x)
        {
        case 0:
        case 1:
            cout << "X is either 0 or 1" << endl;
            break;
        case 2:
        case 3:
            cout << "X is either 2 or 3" << endl;
            break;
        }

  12. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The hypothetical switch you began with wouldn't print two messages either.

    For this particular example, why not the following:
    Code:
    #include <iostream>
    
    int main()
    {
        int x = 1;
        if (x > 0 && x <= 3) std::cout << "X is either " << x-1 << " or " << x << '\n';
        if (x >= 0 && x < 3) std::cout << "X is either " << x << " or " << x+1 << '\n';
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #13
    Registered User
    Join Date
    Jun 2008
    Posts
    5
    Quote Originally Posted by Shaun32887 View Post
    Yeah, you're right. I was thinking of another less efficient way to write the if statements.



    Still, I would have the same problem.


    Code:
    #include <iostream>
    using namespace std;
    
    int main( )
    {
        int x = 1;
    
        if (x == 0 ||x == 1)
            cout << "X is either 0 or 1" << endl;
        else if (x == 1 ||x == 2)
            cout << "X is either 1 or 2" << endl;
        else if (x == 2 ||x == 3)
            cout << "X is either 2 or 3" << endl;
        system("pause");
        return 0;
    }
    This would still only display

    "X is either 0 or 1"

    Since it won't execute ALL the satisfied branches, just the 1st satisfied branch.


    I think the conditional fallthrough might be my solution.
    Remove the two 'else' statements and just use the 'if' and it should be what you want.
    Last edited by Caverage; 08-01-2008 at 05:36 PM.

  14. #14
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Quote Originally Posted by anon View Post
    The hypothetical switch you began with wouldn't print two messages either.
    I know that, that's why I came here to ask the question.

    I should also add that the point of the program wasn't to display those statements. Like I said, it was only a toy program, the concept was hitting two cases in one pass.

  15. #15
    Registered User
    Join Date
    Jun 2008
    Location
    Houston, Texas
    Posts
    43
    Quote Originally Posted by Caverage View Post
    Remove the two 'else' statements and just use the 'if' and it should be what you want.
    Yeah... that's gonna work. Not sure why I didn't think of it myself, but that definitely works.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  2. While Statements Question
    By thekautz in forum C++ Programming
    Replies: 4
    Last Post: 11-09-2008, 02:48 PM
  3. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  4. i need alil help with my switch question
    By datainjector in forum C Programming
    Replies: 17
    Last Post: 06-29-2002, 08:14 PM
  5. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM