4. What is the result of the following code?
A. OneCode:x=0;
switch(x)
{
case 1: printf( "One" );
case 0: printf( "Zero" );
case 2: printf( "Hello World" );
}
B. Zero
C. Hello World
D. ZeroHello World
why it is D, I thought it should be B
Printable View
4. What is the result of the following code?
A. OneCode:x=0;
switch(x)
{
case 1: printf( "One" );
case 0: printf( "Zero" );
case 2: printf( "Hello World" );
}
B. Zero
C. Hello World
D. ZeroHello World
why it is D, I thought it should be B
Because cases fall-through to the next case unless you use the break statement.
if "break;" statement is used then UR expected option B will be the result
Rahmet
rahmetQuote:
Originally Posted by Salem
so you mean after printing Zero the program doesn`t check if
case (2 == x) and it will print anything after case 0
No it doesn't.
The switch condition is only checked once, and that decides where in the switch statement the program jumps to. After that, the code just executes in a linear fashion like any other code until it comes to the closing brace or a break statement.