-3

I have a code snippet where a do while statement is inside switch condition of case0, by default the case value is case1 but it seems executing case0. The output of the program print is 6. How is this possible, can someone explain the flow of code here. Thanks in advance for your answers.

int main()
{
    int a = 1, t =0,n=2;
    switch(a)
    {
        case 0:
        do
        {
        t++;
        case 4:t++;
        case 3:t++;
        case 2:t++;
        case 1:t++;
        }while(--n>0);
        printf("%d",t);
        
    }
    return(0);
    
}

  • 2
    C and C++ are different programming languages. Compile your code using [GCC](http://gcc.gnu.org/) as `gcc -Wall -Wextra -g` and use the [GDB](https://www.gnu.org/software/gdb/) debugger to understand its behavior. – Basile Starynkevitch Jan 18 '21 at 11:15
  • 1
    It doesn't execute case `0`, case `0` would print `10`. – Yksisarvinen Jan 18 '21 at 11:15
  • 4
    Look at [Duff's device](https://en.wikipedia.org/wiki/Duff%27s_device) – Jarod42 Jan 18 '21 at 11:15
  • 3
    Arguably Duff's Device is an anachronism and an abuse of syntax. It really does not need to exist in a world with optimizing compilers. – tadman Jan 18 '21 at 11:19
  • 1
    "Explain how this existing code works" is not an appropriate question for Stack Overflow. For something that isn't fundamental/textbook-level, try a forum website such as Reddit. The problem is that we can't really go back and forth with you, and we don't have a good way to understand what's preventing you from understanding the code already. – Karl Knechtel Jan 18 '21 at 11:23
  • I'm getting 6 in both [C](https://godbolt.org/z/zo5Y88) and [C++](https://godbolt.org/z/PY8T5h). Please describe how they work differently? – Ted Lyngmo Jan 18 '21 at 11:27
  • Why does this program compile? – August Karlstrom Jan 18 '21 at 11:36
  • @AugustKarlstrom It looks ugly, but it's valid. :-) (see my C and C++ examples above for a slightly less ugly formatting) – Ted Lyngmo Jan 18 '21 at 11:40
  • 1
    @AugustKarlstrom switch ... case has a very relaxed specification. But in the 21st century, we should not be using this kind of the constructs as it makes no sense, is error-prone and it makes programs really hard to read. Micro optimizations in the era of the optimizing compilers and powerful computers make no sense at all. – 0___________ Jan 18 '21 at 11:43
  • I don't object to the explanations in the answers, but I wonder what "_Do while inside switch statement works differently in c_" really means. I thought this was question about a difference between the tagged languages (C and C++) but it seems the question didn't have anything to do with differences? – Ted Lyngmo Jan 18 '21 at 12:16

3 Answers3

2

Since a is 1 in the beginning, case 1 will be executed. Then the loop condition is satisfied, thus it will loop again and execute t++; and all other cases until it tests the loop condition again and breaks the loop.

To get out of the switch, use the break command before every case.

1

This is known at Duff's device.

case are mostly only label.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

Switch cases are similar to labels for goto.
You start at case 1, which is inside the loop – effectively using that as your starting point – and then execute the loop normally while "falling through" the cases as you go.

Here is the equivalent using goto:

int main()
{
    int a = 1, t =0,n=2;
    if (a == 0)
        goto case_0;
    if (a == 1)
        goto case_1;
    if (a == 2)
        goto case_2;
    if (a == 3)
        goto case_3;
    if (a == 4)
        goto case_4;
            
  case_0:
    do {
      t++;
  case_4:
      t++;
  case_3:
      t++;
  case_2:
      t++;
  case_1:
      t++;
    } while (--n > 0);
    
    printf("%d",t);
}

(The actual generated code might use a jump table rather than a series of conditionals, but the behaviour is the same.)

molbdnilo
  • 64,751
  • 3
  • 43
  • 82