-2

I was trying to understand the working of switch in C, and after browsing through some of the answers on SO, I fumbled upon the following piece of answer on SO :-

Basically, a switch acts like a goto to the appropriate label -- intervening statements aren't executed. Variable definitions (which actually happen at compile time) do happen, but if they contain initialization, that's skipped too.

How does switch statement work?

Can anybody please explain the meaning of this statement or provide some more insight into the working of switch.
`

Community
  • 1
  • 1
Pratik Singhal
  • 6,283
  • 10
  • 55
  • 97
  • What specifically are you having difficulties with from the link you posted? The accepted answer along with the statement you have highlighted seem clear enough. – shree.pat18 May 12 '14 at 05:40
  • Are you looking for an explanation of how to use it? For an explanation how it's implemented internally? For philosophical ruminations on its similarity to goto statements? – ruakh May 12 '14 at 05:40
  • I am not able to understand how this particular answers that problem in the link. – Pratik Singhal May 12 '14 at 05:41
  • A code example could be really useful to demonstrate the truth of this statement. – Pratik Singhal May 12 '14 at 05:42
  • @ps06756, There are code examples in the other answers to the post that you linked. – merlin2011 May 12 '14 at 05:42
  • @merlin2011 There are code examples for other answers. I am just looking for a code snippet demonstrating this answer. I am unable to think of it myself. – Pratik Singhal May 12 '14 at 05:46
  • @ps06756, Very well, I will write a short example. – merlin2011 May 12 '14 at 05:47
  • @ps06756, I have shown you two code samples, one with `goto` and one with `switch`. In both cases, variable definition happens, but variable initialization is skipped. – merlin2011 May 12 '14 at 05:52

2 Answers2

2

Here is an example of a switch statement, directly copied from this answer.

#include <stdio.h>

int main(){
    int expr = 1;
    switch (expr)
    {
        int i = 4;
        f(i);
        case 0:
        i = 17;
        /* falls through into default code */
        default:
        printf("%d\n", i);
    }
}

Here is an equivalent code sample, written with goto.

#include <stdio.h>

int main(){
    int expr = 1;
    if (expr == 0) goto label0;
    else goto label1;

    int i = 4;
    f(i);
    label0:
    i = 17;

    label1:
    printf("%d\n", i);
}

Observe that, in both cases, when expr is initialized to 1, the initialization statement int i = 4 is not executed, so i is not initialized. However, i is defined, just as the quote in the question mentions.

Community
  • 1
  • 1
merlin2011
  • 71,677
  • 44
  • 195
  • 329
1

Without switch:

  if(1 == x)
     goto LABEL_A;
  else if(2 == x)
     goto LABEL_B;
  else if(10 == x)
     goto LABEL_C;
  else
     goto LABEL_D;

LABEL_A:
  /* Do A */;
  goto BREAK;

LABEL_B:
  /* Do B */;
  goto BREAK;

LABEL_C:
  /* Do C */;
  goto BREAK;

LABEL_D:
  /* Do D */;
  goto BREAK;

BREAK:

With switch:

switch(x)
  {
  case 1:
     /* Do A */;
     break;

  case 2:
     /* Do B */;
     break;

  case 10:
     /* Do C */;
     break;

  default:
     /* Do D */;
     break;
  }
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28