为什么下面的代码会显示Nooutput?
以下代码是过去大学考试中的 MCQ 问题:您能否也向我解释这两个 if 语句背后的逻辑。
bool s=37%50<=0;
int x=2;
if(s)
if(x>0)
cout<<"Great";
else
cout<<"News";
此代码显示:
a)Great News
b)Great
c)News
d)No output
回答
上面的写法是一样的:
bool s= 37%50 <= 0;
int x=2;
if(s) {
if(x>0) {
cout<<"great";
} else {
cout<<"news";
}
}
由于s将是false(37%50不是<= 0),因此不会显示任何内容。