是的,是的4,让我们格式化代码(右缩进)并查看:int i = 0; // i == 0if (i == 0) // i == 0 i++; // i == 1i++; // i == 2if (i == 3) // i == 2 i += 2; // doesn't enter (since i != 3)i += 2; // i == 4
您需要使用大括号 { } 来表示超出单行条件的任何内容,或者仅在条件为 true 时执行后的第一行代码。/*for example would be if (i == 0) { i++; i++; }*/int i = 0;//this is trueif (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 falseif (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 = 4Console.WriteLine(i);//and that's what prints