1
#include <iostream>

int main() {
    int n = 3, i=0;

    switch (n % 2) 
    {
    case 0: 
        do {
            ++i;
    case 1:
            ++i;
        } while (--n > 0);
    }
    std::cout << i;
}

Why is the output 5 and not 6 ? Can someone explain it to me more detailed ?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • 4
    Look at [Duff's_device](https://en.wikipedia.org/wiki/Duff%27s_device). – Jarod42 Jul 06 '23 at 16:20
  • 5
    That's a variant of [Duff's device](https://en.wikipedia.org/wiki/Duff%27s_device). Just never use code like that, and sites that uses such code shouldn't really be used for anything useful, except perhaps as an example of bad code. – Some programmer dude Jul 06 '23 at 16:20
  • 3
    Use a debugger and step through the code. That will show you why the output is 5 and not 6. – Eljay Jul 06 '23 at 16:22
  • 4
    If you want to know how it works, then read about Duff's device (previously linked) and step through it line by line in a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) while monitoring variables and their values to see what's really going on. – Some programmer dude Jul 06 '23 at 16:23
  • @PepijnKramer -- a `switch` statement isn't special; you can put a label inside its block and jump to that label from outside, just like any other block (except for a function body). And a `case` statement is just a label. That's why, for example, code without a `break` falls through to the next case. Both statements are far less expressive than they might seem. – Pete Becker Jul 06 '23 at 16:30
  • 1
    Duff's device (see links above) is a method to implement loop unrolling, which is an optimization technique. It's definately not recommended to use such code without a very good reason (if at all). – wohlstad Jul 06 '23 at 16:33
  • 1
    Please explain your expectation of 6 as the result. Then answering by pointing out the misunderstanding our by providing reasonsing why you must have found an error in your compiler is much easier. – Yunnosch Jul 06 '23 at 16:48

2 Answers2

3

Basically, when you enter the 'switch' you jump inside the loop body. Then the loop is executed ignoring the label 'case 1'.

It is like executing the following lines of code.

++i;
--n;
++i;
++i;
--n;
++i;
++i;
--n;

I never suggest a code style like that, because it is harder to maintain. I prefer instead a structured code. The style of your example reminds me the C64 Basic or Assembly.

While this code style is deprecable, the C language (C++ too) is flexible enough to allow you this. Take care because jumping randomly around could decrease the optimizer performance. At the end your code could run slower than the structured solution.

Gildos
  • 41
  • 2
0

as n%2 = 1 then after the switch statement yo go to the case 1 which is in do while, so you entered in the do while then these lines will be exectued

++i // then i = 1
--n // now n = 2 which is >0
++i // i = 2
++i // i = 3
--n // now n = 1 which is > 0
++i // i = 4
++i // i = 5
--n //  now n = 0 and does not satisfy the condition so the loop breaks and i = 5
Nishant
  • 19
  • 5
  • *then these lines will be exectued* But why are they executed? What happened in the conditions to cause this "unexpected" behaviour? Without an explanation of why, the answer is incomplete. – user4581301 Jul 06 '23 at 17:28
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 10 '23 at 08:32