Switch statement

This is a discussion on Switch statement within the C++ Programming forums, part of the General Programming Boards category; Is it possible to have a switch statement use more than one variable? i.e., something like: Code: switch (n && ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Switch statement

    Is it possible to have a switch statement use more than one variable? i.e., something like:

    Code:
    switch (n && nn) {
    
      case 1 && 2:
        cout<< "n was 1 and nn was 2." <<endl;
        break;
    
      case 2 && 1: 
        cout<< "n was 2 and nn was 1." <<endl;
        break;
    
      default:
        cout<< "n wasn't 1 or 2 and neither was nn." <<endl;
    
    }
    Last edited by Programmer_P; 07-23-2010 at 07:00 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    No, not in the sense you expect, but you can use nested switches. Or two followed by each other.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    No, not in the sense you expect, but you can use nested switches. Or two followed by each other.
    Ok. So something like this then:

    Code:
    switch(n) {
    
      case 1:
        switch(nn) {
          case 2:
            cout<< "n is 1 and nn is 2." <<endl;
          default:
            cout<< "n is 1 and nn is not 2." <<endl;
        }
    
      case 2:
        switch(nn) {
          case 1:
            cout<< "n is 2 and nn is 1." <<endl;
          default:
            cout<< "n is 2 and nn is not 1." <<endl;
        }
    
       default:
         cout<< "n is neither 1 or 2." <<endl;
    }
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    That works if you must have it so. It all depends on what you want to do.
    Sometimes you can simplify it with some logic twists.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I guess you could cram two small ints into a regular int and use macro magic to have this functionality:
    Code:
    switch( SHORT_PAIR(n,nn) )
    {
       case SHORT_PAIR(1,1):
          // . . .
          break;
       case SHORT_PAIR(1,2):
    // . . . etc
    };
    But that would be a hackish, inelegant solution.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Speaking of which, with constexpr, we should be able to do the same thing without macros in C++0x.
    Ah, but we could also use some template trickery for the cases and a function for the switch header, too.
    I suppose I could demonstrate if there are any interested parties.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I thought there was a template solution, but I'm not clear on how you make a template evaluate to only a constant expression. I'm interested in your demonstration.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    But your case labels are constant expressions, no?
    Assuming we're stuffing two shorts into an int:
    Code:
    template<short N1, short N2> struct ShortPair { static const int N = (N1 << 16) + N2; };
    
    int MakeShortPair(short N1, short N2)
    {
        return (N1 << 16) + N2;
    }
    
    int main()
    {
    	switch (MakeShortPair(10, 20))
    	{
    		case ShortPair<10, 20>::N: /* Do something */ ; break;
    	}
    }
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I guess Elysia meant something like this:

    Code:
    #include <iostream>
    using namespace std;
    
    unsigned combine_small_values(int a, int b)
    {
        return a | (b << 16);
    }
    
    template <unsigned a, unsigned b>
    struct combine_small_values_at_compile_time
    {
        static const unsigned value = a | (b << 16);
    };
    
    int main()
    {
        int n = 2, nn = 1;
        switch (combine_small_values(n, nn)) {
    
          case combine_small_values_at_compile_time<1, 2>::value:
            cout<< "n was 1 and nn was 2." <<endl;
            break;
    
          case combine_small_values_at_compile_time<2, 1>::value:
            cout<< "n was 2 and nn was 1." <<endl;
            break;
    
          default:
            cout<< "n wasn't 1 or 2 and neither was nn." <<endl;
    
        }
    }
    Perhaps also try something along the lines of:
    Code:
        if ((n == 1 || n == 2) && (nn == 1 || nn == 2)) {
            cout << "n was " << n << " and nn was " << nn << ".\n";
        }
        else {
            cout << "n or nn was neither 1 nor 2.\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).

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Cool. I didn't think of a template struct with a static const member. That opens up my head a bit.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. issue with switch statement
    By bluetxxth in forum C Programming
    Replies: 14
    Last Post: 02-24-2010, 09:01 AM
  2. Switch Statement Errors
    By melodia in forum C Programming
    Replies: 7
    Last Post: 10-25-2009, 03:51 PM
  3. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  4. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  5. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21