I saw this code and I expected the output to be 6, but I got 17.
int x = 3;
switch (x)
{
case 1: { x += x; }
case 3: { x += x; }
case 5: { x += x; }
default: { x += 5; }
}
std::cout << x;
Why does x
become 17? Shouldn't it become 6 by selecting the case 3 and exiting the switch statement?
Even if it would go through the default case after the case 3, x
would only be 11. The only way I see that x
would become 17 is by going through case (1 & 3) or (3 & 5) + the default case. I don't see why it would work like that though.
Any direction on what's happening step-by-step would be great!
break
break
is forgot.