Multiple Control Variables in a Switch Statement (C)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Silent1Mezzo
    New Member
    • Feb 2007
    • 208

    Multiple Control Variables in a Switch Statement (C)

    Is it possible to have multiple control variables in a switch statement?

    For example:

    [CODE=c]switch(a , b)
    {
    case(a>b):
    case(b<a):
    case(a==b):
    }[/CODE]

    (I know you can't separate it by a comma...I've tried....I also know that there aren't any breaks or anythign like that. I'm not asking about syntax of a switch statement, just if you can have multiple control variables) but is there any other way to do this?
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by Silent1Mezzo
    Is it possible to have multiple control variables in a switch statement?

    For example:

    Code:
    switch(a , b)
    {
           case(a>b):
           case(b<a):
           case(a==b):
    }
    (I know you can't separate it by a comma (I've tried) but is there any other way to do this?
    Nope, I don't think so. You can easily do the same thing with if statements though:
    [code=cpp]
    if (a > b){}
    elif (a < b){}
    elif (a == b){}
    [/code]

    Comment

    • Silent1Mezzo
      New Member
      • Feb 2007
      • 208

      #3
      Originally posted by ilikepython
      Nope, I don't think so. You can easily do the same thing with if statements though:
      [code=cpp]
      if (a > b){}
      elif (a < b){}
      elif (a == b){}
      [/code]
      True...Hmm ok thanks

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        else if

        not

        elif


        Unless of course they are preprocessor statements

        Comment

        • ilikepython
          Recognized Expert Contributor
          • Feb 2007
          • 844

          #5
          Originally posted by Banfa
          else if

          not

          elif


          Unless of course they are preprocessor statements
          Oh, Duuh!! It's like that in python, sorry.

          Comment

          • Savage
            Recognized Expert Top Contributor
            • Feb 2007
            • 1759

            #6
            Originally posted by ilikepython
            Oh, Duuh!! It's like that in python, sorry.
            Haha

            I just hate when I mix laanguages.

            Savage

            Comment

            • Silent1Mezzo
              New Member
              • Feb 2007
              • 208

              #7
              I knew what you were saying ilikepython

              Comment

              • AdrianH
                Recognized Expert Top Contributor
                • Feb 2007
                • 1251

                #8
                switch only takes one 'parameter' which is an integral number (used to be transformed to an int). When using an enum, some compilers will complain if you havn't covered all of the enums.

                This is all done this way so that the compiler can optimise the jump to the correct code useing a jump table if it is densely packed or cluster packed, or can degrade into something resembling an if (num == value1) {} else if (num == value2) ... if the values are spread out significantly.


                Adrian

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  There are various tricks you can pull with switch statements, here's 1 I've seen in a number of projects

                  [code=c++]
                  typedef struct {
                  int member1;
                  int member2;
                  } MyStruct;

                  void MyFunction(void )
                  {
                  switch(true)
                  {
                  case (sizeof MyStruct == 8):
                  case 0:
                  break;
                  }
                  }[/code]

                  Comment

                  • AdrianH
                    Recognized Expert Top Contributor
                    • Feb 2007
                    • 1251

                    #10
                    Originally posted by Banfa
                    There are various tricks you can pull with switch statements, here's 1 I've seen in a number of projects

                    [code=c++]
                    typedef struct {
                    int member1;
                    int member2;
                    } MyStruct;

                    void MyFunction(void )
                    {
                    switch(true)
                    {
                    case (sizeof MyStruct == 8):
                    case 0:
                    break;
                    }
                    }[/code]
                    Cute, but what exactly does that get you except code obfustication?


                    Adrian

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by AdrianH
                      Cute, but what exactly does that get you except code obfustication?
                      It's a compile time check for structure size, it only compiles if sizeof MyStruct == 8.

                      In 1 or 2 obscurish instances there has been a requirement that a structure wasn't changed and that is the closest thing you can get to automated checking.

                      Obviously when in use you put a large comment in explaining what to do or who to see in case of an error generated there.

                      Comment

                      • AdrianH
                        Recognized Expert Top Contributor
                        • Feb 2007
                        • 1251

                        #12
                        Originally posted by Banfa
                        It's a compile time check for structure size, it only compiles if sizeof MyStruct == 8.

                        In 1 or 2 obscurish instances there has been a requirement that a structure wasn't changed and that is the closest thing you can get to automated checking.

                        Obviously when in use you put a large comment in explaining what to do or who to see in case of an error generated there.
                        Ah, I see. I've seen something else like that.. mmmm
                        [code=c]
                        typedef struct {
                        int member1;
                        int member2;
                        } MyStruct;

                        void MyFunction(void )
                        {
                        int test[(sizeof MyStruct == 8) ? 1 : -1];
                        }
                        [/code]
                        Well actually that is something like the expanded version, it was done using macros:
                        [code=c]
                        #define CAT(x,y) x ## y
                        #define PREASSERT(x) \
                        static void CAT(PREASSERT_, __LINE__)(void) \
                        { \
                        int test[(x) ? 1 : -1]; \
                        }

                        PREASSERT(sizeo f MyStruct == 8)
                        [/code]or something like that.

                        Don't try this at home fokes, I'm a trained professional. ;) :D


                        Adrian

                        Comment

                        • Savage
                          Recognized Expert Top Contributor
                          • Feb 2007
                          • 1759

                          #13
                          Originally posted by AdrianH
                          Don't try this at home fokes, I'm a trained professional. ;) :D
                          Hahaha,I like this..

                          Savage

                          Comment

                          • Banfa
                            Recognized Expert Expert
                            • Feb 2006
                            • 9067

                            #14
                            Originally posted by AdrianH
                            Don't try this at home fokes, I'm a trained professional. ;) :D
                            Yes and I am Al Borland :D

                            Comment

                            • Savage
                              Recognized Expert Top Contributor
                              • Feb 2007
                              • 1759

                              #15
                              Originally posted by Banfa
                              Yes and I am Al Borland :D
                              U are?

                              :)

                              Savage

                              Comment

                              Working...