下面两段 C++ 代码都是用于求 1-10 的和,其运行结果相同。通常说来,for 循环都可以用 while 循环实现。
int tnt;
int i;
tnt = 0;
for (i = 1; i < 10 + 1; i++)
tnt += i;
cout << tnt << endl;
int tnt;
int i;
tnt = 0;
i = 1;
while (i <= 10){
tnt += i;
i += 1;
}
cout << tnt << endl;