int ch;
cin>>ch;
switch(ch)
{
case 1:
cout<<"one";
break;
case 2:
cout<<"two";
break;
case 3:
cout<<"three";
break;
}
When we trace the above code, I choose value 3 for the variable ch, then the compiler switches to case 3 and print statement "three". After printing this the compiler goes to case 1's break instead of going case 3's break.why?
The debugger in step-through mode. I'm guessing that it's some compiler-specific "optimizati on" or the closing of the switch statement (when you step through, a lot of the time it bounces back up to the check to close the loop). However, the case 3 break is definitely not ignored - you can manipulate its place (put things before and after), and see that it is definitely being caught there.
The debugger in step-through mode. I'm guessing that it's some compiler-specific "optimizati on" or the closing of the switch statement (when you step through, a lot of the time it bounces back up to the check to close the loop). However, the case 3 break is definitely not ignored - you can manipulate its place (put things before and after), and see that it is definitely being caught there.
It's the code generator to blame and the short term memory of it: once the jump
out of the switch statement has been found (possibly it was a 'short' and 'relative'
jump, jump to that jump instruction and all will be fine. Possibly a later code
generating/optimizing pass over the code can remove cascaded jumps as well.
Comment