Question regarding Switch Case - thnx

Could somebody please explain why the answer is D and not B. I suspect the reason is because there is no Break. Thanks

4. What is the result of the following code?
int x=0;
switch(x)
{
case 1: printf( "One" );
case 0: printf( "Zero" );
case 2: printf( "Hello World" );
}
A. One
B. Zero
C. Hello World
D. ZeroHello World
Last edited on
No "break;" statements after your conditions. Also it's worth noting that the reason that "case 1:" did not execute is because it is postioned above the case that was met, if you were to change 'x' to 1 then all three would execute.
Last edited on
Case statements will fall through to the next case if there is no break statement;

So you code will print "Zero" and just carry on into the next case and print "hello World";


Topic archived. No new replies allowed.