所以我正在阅读 C++ Primer(第 5 版),这是他们提供的用于解释 while 语句的示例代码:
#include <iostream>
int main()
{
int sum = 0, val = 1;
// keep executing the while as long as val is less than or equal to 10
while (val <=10) {
sum += val; // assigns sum + val to sum
++val; // add 1 to val
}
std::cout << "Sum of 1 to 10 inclusive is " << sum << std::endl;
return 0;
}
这是命令行提示符下的程序:
我只是不明白 55 是从哪里来的..
不应该是:
sum = 0
var = 1
sum = 0 + 1
var = 1 + 1
-snip-
sum = 6
var = 4
那么它不应该打印 6 吗?我真的很困惑。