代码文件位whilecount.cpp
1 #include <iostream> 2 3 int main() 4 { 5 int sum = 0, val = 1; 6 // val小于等于10时,while后的代码块会持续循环运行 7 while (val <= 10) { 8 sum += val; // 等价于 将sum+val的值赋值给sum 9 ++val; // val加1 10 } 11 std::cout << "Sum of 1 to 10 inclusive is " 12 << sum << std::endl; 13 14 return 0; 15 }
编译运行