
最后运行出来的结果是0
为啥不是3?。“.!、…;”-/:;—()
因为if和else if条件都不满足,所以y还是第一次赋的值0。
测试如下:
参考链接:
https://blog.csdn.net/qq_31243065/article/details/80924922
https://blog.csdn.net/wanyhong/article/details/132844534
https://blog.csdn.net/sexyluna/article/details/81543689
https://blog.csdn.net/qq_42580901/article/details/109697454
#include <iostream>
using namespace std;
int main(void){
int x=1,y=0; // 这里定义变量x和y时,同时分别给它们赋了初始值1和0
// https://blog.csdn.net/qq_31243065/article/details/80924922
// https://blog.csdn.net/wanyhong/article/details/132844534
// https://blog.csdn.net/sexyluna/article/details/81543689
// https://blog.csdn.net/qq_42580901/article/details/109697454
if(!x){ // 这里的if对x取非,x为1,对应于真,然后取非,if的条件就变为假,所以if里语句不会执行
y++;
}else if(x==0){ // 前面的if条件不满足,if里的语句未执行, 再往下判断else if的条件,因为x为1,所以x==0为假,所以else if条件为假,else if里的语句也没有执行
if(x){
y+=2;
}else{ // 这里的else分支和最近的没有匹配的if进行匹配,即上面的if(x),它们同处于else if分支下,因为else if的条件为假,所以这里的else里的语句不会被执行
y+=3;
}
}
cout<<y; // 因为前面的if和else if里的语句都没有执行,所以y还是其初始赋值0,所以最后打印y的值就是0
return 0;
}
