2 回答
TA贡献1803条经验 获得超3个赞
是的,是的4,让我们格式化代码(右缩进)并查看:
int i = 0; // i == 0
if (i == 0) // i == 0
i++; // i == 1
i++; // i == 2
if (i == 3) // i == 2
i += 2; // doesn't enter (since i != 3)
i += 2; // i == 4
TA贡献1874条经验 获得超12个赞
您需要使用大括号 { } 来表示超出单行条件的任何内容,或者仅在条件为 true 时执行后的第一行代码。
/*
for example would be
if (i == 0)
{
i++;
i++;
}
*/
int i = 0;
//this is true
if (i == 0)
i++; // so only this line gets executed i = 1
i++; // this will get executed no matter what. i = 2
//at this point i = 2 so the conditional is false
if (i == 3)
i += 2; // this line doesn't get executed
i += 2; /* this is not in curly brackets { } so it will get executed no matter what the conditional returns as .. so i = 4*/
//i = 4
Console.WriteLine(i);
//and that's what prints
- 2 回答
- 0 关注
- 96 浏览
添加回答
举报