开发者

what is the output if x == 42 [closed]

开发者_JS百科 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.
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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜