Switch case statement

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sam_cit@yahoo.co.in

    Switch case statement

    Hi Everyone,

    In the following code, i have a common action for three switch cases,
    is there any other better way to write the three values in a single
    case?

    Thanks in advance

    #include <stdio.h>

    int main()
    {
    int i = 1;
    switch(i)
    {
    case 2: printf("before. ..\n");
    break;
    case 4:
    case 5:
    case 6: printf("in...\n ");
    break;
    case 7: printf("out...\ n");
    break;
    }
    return(0);
    }

  • Sharath

    #2
    Re: Switch case statement


    sam_cit@yahoo.c o.in wrote:
    Hi Everyone,
    >
    In the following code, i have a common action for three switch cases,
    is there any other better way to write the three values in a single
    case?
    >
    Thanks in advance
    >
    #include <stdio.h>
    >
    int main()
    {
    int i = 1;
    switch(i)
    {
    case 2: printf("before. ..\n");
    break;
    case 4:
    case 5:
    case 6: printf("in...\n ");
    break;
    case 7: printf("out...\ n");
    break;
    }
    return(0);
    }

    Not better way unless you use any compiler provided extensions to give
    range in the case statement.
    Read:


    Comment

    • Chris Dollin

      #3
      Re: Switch case statement

      sam_cit@yahoo.c o.in wrote:
      Hi Everyone,
      >
      In the following code, i have a common action for three switch cases,
      is there any other better way to write the three values in a single
      case?
      No.

      Also: they're not /values/, they're /statements/. It isn't a "single
      case": it's three /case labels/.

      What problem do you fear here?
      #include <stdio.h>
      >
      int main()
      {
      int i = 1;
      switch(i)
      {
      case 2: printf("before. ..\n");
      break;
      case 4:
      case 5:
      case 6: printf("in...\n ");
      break;
      case 7: printf("out...\ n");
      break;
      }
      return(0);
      }
      This body is best written

      return 0;

      since, `i` being `1`, the switch doesn't do anything at all ...
      one of the hazards of on-the-fly examples.

      --
      Chris "hopefully not Pyecroft" Dollin
      Scoring, bah. If I want scoring I'll go play /Age of Steam/.

      Comment

      • CBFalconer

        #4
        Re: Switch case statement

        sam_cit@yahoo.c o.in wrote:
        >
        In the following code, i have a common action for three switch
        cases, is there any other better way to write the three values
        in a single case?
        >
        Thanks in advance
        >
        #include <stdio.h>
        >
        int main()
        {
        int i = 1;
        switch(i)
        {
        case 2: printf("before. ..\n");
        break;
        case 4:
        case 5:
        case 6: printf("in...\n ");
        break;
        case 7: printf("out...\ n");
        break;
        }
        return(0);
        }
        Yes :-)

        #include <stdio.h>

        int main(void) {
        int i;

        for (i = 1; i < 8; i++) {
        switch(i) {
        case 2: printf("before" ); break;
        case 4:
        case 5:
        case 6: printf("in"); break;
        case 7: printf("out"); break;
        default: printf("nada"); break;
        }
        printf(" %d\n", i);
        }
        return(0);
        }

        --
        Chuck F (cbfalconer at maineline dot net)
        Available for consulting/temporary embedded and systems.
        <http://cbfalconer.home .att.net>


        Comment

        • Keith Thompson

          #5
          Re: Switch case statement

          sam_cit@yahoo.c o.in writes:
          In the following code, i have a common action for three switch cases,
          is there any other better way to write the three values in a single
          case?
          >
          #include <stdio.h>
          >
          int main()
          {
          int i = 1;
          switch(i)
          {
          case 2: printf("before. ..\n");
          break;
          case 4:
          case 5:
          case 6: printf("in...\n ");
          break;
          case 7: printf("out...\ n");
          break;
          }
          return(0);
          }
          No, there's no standard syntax for specifying a range in a case
          statement (yes, it might be nice if there were). For only 3 cases,
          enumerating each case is probably the best approach. For a larger
          range, say a few hundred cases, you can just use a series of if/else
          statements. The following is equivalent to your program, but can
          easily be modified to cover larger ranges:

          #include <stdio.h>
          int main(void)
          {
          int i = 1;
          if (i == 2) {
          printf("before. ..\n");
          }
          else if (i >= 4 && i <= 6) {
          printf("in...\n ");
          }
          else if (i == 7) {
          printf("out...\ n");
          }
          return 0;
          }

          If you have a combination of individual values and large ranges, you
          can even combine a switch statement with an if statement:

          #include <stdio.h>
          int main(void)
          {
          int i = 1;
          switch (i) {
          case 2:
          printf("before. ..\n");
          break;
          case 7:
          printf("out...\ n");
          break;
          default:
          if (i >= 4 && i <= 6) {
          printf("in...\n ");
          }
          break;
          }
          return 0;
          }

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Christopher Benson-Manica

            #6
            Re: Switch case statement

            sam_cit@yahoo.c o.in wrote:
            In the following code, i have a common action for three switch cases,
            is there any other better way to write the three values in a single
            case?
            Sure!

            #include <stdio.h>

            #define CASE3(x) case x: case x+1: case x+2:

            int main(void) {
            int i=1;
            switch(i) {
            case 1:
            printf( "1\n" );
            break;
            CASE3(2) /* From 2 to 4 */
            printf( "2 to 4\n" );
            break;
            default:
            printf( "default\n" );
            }
            return 0;
            }

            The implementation of the CASE100 macro is left as an exercise :-)

            --
            C. Benson Manica | I *should* know what I'm talking about - if I
            cbmanica(at)gma il.com | don't, I need to know. Flames welcome.

            Comment

            • Eric Sosman

              #7
              Re: Switch case statement

              Christopher Benson-Manica wrote On 01/03/07 12:14,:
              sam_cit@yahoo.c o.in wrote:
              >
              >
              >In the following code, i have a common action for three switch cases,
              >>is there any other better way to write the three values in a single
              >>case?
              >
              >
              Sure!
              >
              #include <stdio.h>
              >
              #define CASE3(x) case x: case x+1: case x+2:
              >
              int main(void) {
              int i=1;
              switch(i) {
              case 1:
              printf( "1\n" );
              break;
              CASE3(2) /* From 2 to 4 */
              printf( "2 to 4\n" );
              break;
              default:
              printf( "default\n" );
              }
              return 0;
              }
              >
              The implementation of the CASE100 macro is left as an exercise :-)
              #define CASE100(n) CASE64(n) CASE36((n)+64)
              #define CASE64(n) CASE32(n) CASE32((n)+32)
              #define CASE36(n) CASE32(n) CASE4((n)+32)
              #define CASE32(n) CASE16(n) CASE16((n)+16)
              #define CASE16(n) CASE8(n) CASE8((n)+8)
              #define CASE8(n) CASE4(n) CASE4((n)+4)
              #define CASE4(n) CASE2(n) CASE2((n)+2)
              #define CASE2(n) case n: case (n)+1:

              --
              Eric.Sosman@sun .com

              Comment

              • Keith Thompson

                #8
                Re: Switch case statement

                Christopher Benson-Manica <ataru@ukato.fr eeshell.orgwrit es:
                sam_cit@yahoo.c o.in wrote:
                > In the following code, i have a common action for three switch cases,
                >is there any other better way to write the three values in a single
                >case?
                >
                Sure!
                >
                #include <stdio.h>
                >
                #define CASE3(x) case x: case x+1: case x+2:
                >
                int main(void) {
                int i=1;
                switch(i) {
                case 1:
                printf( "1\n" );
                break;
                CASE3(2) /* From 2 to 4 */
                printf( "2 to 4\n" );
                break;
                default:
                printf( "default\n" );
                }
                return 0;
                }
                >
                The implementation of the CASE100 macro is left as an exercise :-)
                The OP asked for a *better* way to write it; I don't think your
                solution qualifies.

                --
                Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
                We must do something. This is something. Therefore, we must do this.

                Comment

                Working...