what is the output if x == 42 [closed]
switch (x % 5)
{
case 0: cout << x++ << '';
case 1: cout << x-- << '';
break;
case 2: cout << ++x << '';
case 3: cout << --x << '';
default: cout << 2*x << '';
}
Since ''
is an empty character constant, you will get an error. So there will be no output from the program.
Here is the explanation:
if x is 42, x%5 = 2. Therefore, case 2 will be executed. Since there is no break, case 3 and default will also be executed.
Therefore output is:
43 42 84
43, 42, 84 when compiled and run on my machine.
Looks like homework ¬_¬
You hit case two and then drop through hitting case 3 and default due to the absence of breaks;
http://codepad.org/asP1EbPw - was it so difficult to check it?
You must put a
break;
at the end of each case. If you don't do this your x var will have a weird value because enter in two or more switch's cases.
精彩评论