Behavior of if construct in switch case defualt construct.

Discussion in 'C Programming' started by Mukesh, Mar 26, 2010.

  1. Mukesh

    Mukesh Guest

    Hello,

    Please help in how the following program works(i.e behavior of if
    statement inside switch default statement.) :



    /**
    * test.c - Test for switch construct.
    */
    #include <stdio.h>

    int main()
    {
    int i = 10 ;
    printf("\n hello world\n");

    switch (i) {
    printf("\n In switch statement\n");
    case 1:
    printf("\n You entered: %d \n", i);
    break;
    case 2:

    printf("\n You entered: %d \n", i);
    break;

    default :
    if (9 == i) {
    case 9:
    printf("\n in case 9 entered value: %d\n", i);
    break;
    case 10:
    printf("\n in case 10, entered value: %d\n", i);
    break;
    }
    }

    return 0;
    }

    Output:
    ++++++++
    hello world

    in case 10, entered value: 10
     
    Mukesh, Mar 26, 2010
    #1
    1. Advertising

  2. Mukesh

    bartc Guest

    Mukesh wrote:

    > Please help in how the following program works(i.e behavior of if
    > statement inside switch default statement.) :


    > /**
    > * test.c - Test for switch construct.
    > */
    > #include <stdio.h>
    >
    > int main()
    > {
    > int i = 10 ;
    > printf("\n hello world\n");
    >
    > switch (i) {
    > printf("\n In switch statement\n");
    > case 1:
    > printf("\n You entered: %d \n", i);
    > break;
    > case 2:
    >
    > printf("\n You entered: %d \n", i);
    > break;
    >
    > default :
    > if (9 == i) {
    > case 9:
    > printf("\n in case 9 entered value: %d\n", i);
    > break;
    > case 10:
    > printf("\n in case 10, entered value: %d\n", i);
    > break;
    > }
    > }
    >
    > return 0;
    > }


    You're jumping into the body of the if-statement, so bypassing the
    condition.

    C's switch statement is even weirder than I thought: making nonsense of the
    default case, and being able to write unreachable code like this.

    > Output:
    > ++++++++
    > hello world
    >



    More interesting is how you managed to produce those "+" symbols in the
    output.

    --
    Bartc
     
    bartc, Mar 26, 2010
    #2
    1. Advertising

  3. Mukesh

    Mukesh Guest

    On Mar 26, 4:11 pm, "bartc" <> wrote:
    > Mukesh wrote:
    > > Please help in how the following program works(i.e behavior of if
    > > statement inside switch default statement.) :
    > > /**
    > > * test.c - Test for switch construct.
    > > */
    > > #include <stdio.h>

    >
    > > int main()
    > > {
    > > int i = 10 ;
    > > printf("\n hello world\n");

    >
    > > switch (i) {
    > >  printf("\n In switch statement\n");
    > > case 1:
    > >     printf("\n You entered: %d \n", i);
    > >     break;
    > > case 2:

    >
    > >     printf("\n You entered: %d \n", i);
    > >     break;

    >
    > > default :
    > >     if (9 == i) {
    > >   case 9:
    > >      printf("\n in case 9 entered value: %d\n", i);
    > >      break;
    > >   case 10:
    > >      printf("\n in case 10, entered value: %d\n", i);
    > >    break;
    > >    }
    > > }

    >
    > > return 0;
    > > }

    >
    > You're jumping into the body of the if-statement, so bypassing the
    > condition.
    >
    > C's switch statement is even weirder than I thought: making nonsense of the
    > default case, and being able to write unreachable code like this.
    >
    > > Output:
    > > ++++++++
    > > hello world

    >
    > More interesting is how you managed to produce those "+" symbols in the
    > output.

    it's added by me ... + symbols not comes with output :)
    >
    > --
    > Bartc
     
    Mukesh, Mar 26, 2010
    #3
  4. Mukesh

    Willem Guest

    Mukesh wrote:
    ) Please help in how the following program works(i.e behavior of if
    ) statement inside switch default statement.) :

    'case' statements are basically the same as goto-labels.
    The switch() basically executes a 'goto' to the matching case label.
    So it 'goto'-s the 'case 10:' label and starts running from there.


    SaSW, Willem
    --
    Disclaimer: I am in no way responsible for any of the statements
    made in the above text. For all I know I might be
    drugged or something..
    No I'm not paranoid. You all think I'm paranoid, don't you !
    #EOT
     
    Willem, Mar 26, 2010
    #4
  5. Mukesh

    Paul N Guest

    On 26 Mar, 10:54, Mukesh <> wrote:
    > Hello,
    >
    > Please help in how the following program works(i.e behavior of if
    > statement inside switch default statement.) :
    >
    > /**
    >  * test.c - Test for switch construct.
    >  */
    > #include <stdio.h>
    >
    > int main()
    > {
    > int i = 10 ;
    > printf("\n hello world\n");
    >
    > switch (i) {
    >   printf("\n In switch statement\n");
    > case 1:
    >      printf("\n You entered: %d \n", i);
    >      break;
    > case 2:
    >
    >      printf("\n You entered: %d \n", i);
    >      break;
    >
    > default :
    >      if (9 == i) {
    >    case 9:
    >       printf("\n in case 9 entered value: %d\n", i);
    >       break;
    >    case 10:
    >       printf("\n in case 10, entered value: %d\n", i);
    >     break;
    >     }
    >
    > }
    >
    > return 0;
    >
    > }
    >
    > Output:
    > ++++++++
    > hello world
    >
    > in case 10, entered value: 10


    No discussion of this topic would be complete wihout mentioning
    "Duff's Device". See http://en.wikipedia.org/wiki/Duff's_device for
    more details.

    Paul.
     
    Paul N, Mar 26, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. MJ Baldwin
    Replies:
    3
    Views:
    2,228
    Steve R.
    Dec 21, 2003
  2. Phillip Vong

    Set defualt date as Week Day before Today.

    Phillip Vong, Dec 11, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    569
    Rad [Visual C# MVP]
    Dec 12, 2006
  3. Michael Mair
    Replies:
    1
    Views:
    428
    S.Tobias
    Jun 14, 2005
  4. Chad
    Replies:
    7
    Views:
    336
    Boltar
    Jan 5, 2007
  5. Replies:
    9
    Views:
    373
    shish
    May 2, 2007
Loading...

Share This Page