Behavior of if construct in switch case defualt construct.

M

Mukesh

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
 
B

bartc

Mukesh said:
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.
 
M

Mukesh

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.


More interesting is how you managed to produce those "+" symbols in the
output.
it's added by me ... + symbols not comes with output :)
 
W

Willem

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
 
P

Paul N

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top