gpt4 book ai didi

有人能解释一下C语言中的for循环吗? for(x = i = 0; i <= 100; i += 2, x += i);

转载 作者:行者123 更新时间:2023-11-30 21:19:05 25 4
gpt4 key购买 nike

for(x = i = 0; i <= 100; i += 2, x += i);

为什么上面的代码将 0 到 102(含)的所有偶数相加到 x 中,而不是下面的代码将 0 到 100(含)的所有偶数相加到 x 中?

for(x = i = 0; i <= 100; i++){
if (!(i % 2))
continue;
x = x + i;
}

最佳答案

如果更清楚的话,第一个片段可以完全翻译为:

i = 0;
x = 0;
while (i <= 100)
{
i +=2;
x += i; //sum of the even numbers from 2 to 102 inclusive
}

第二个是这样的:

i = 0;
x = 0;
while (i <= 100)
{
if (!(i % 2)) continue; //skip even numbers
x += i; //sum of the odd numbers from 1 to 100
i++;
}

关于有人能解释一下C语言中的for循环吗? for(x = i = 0; i <= 100; i += 2, x += i);,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58575107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com