gpt4 book ai didi

c - 为什么在下面的代码中当 i=0 时这个条件失败?

转载 作者:行者123 更新时间:2023-11-30 21:43:52 24 4
gpt4 key购买 nike

为什么当 i 变为 0 时此条件失败?i++ 表示 i 从 -5 变为 -4,因为最初 i 为 -5,条件如何为真?

#include <stdio.h>
int main()
{
static int i = -5;
if (i++) //why does this condition fails when it becomes 0
{
printf("%d ", i);//recurtion starts
main(10);
}
}
<小时/>

for循环中i++++i有什么区别?

最佳答案

Why does this condition fail when i=0

如果 if 语句中的条件返回 false(等于 0),则不会执行该语句。也就是说,仅当条件返回 true(或非零值)时才会执行该语句

if( <condition> )

如果条件返回 false (0),则 if 语句将不会执行。 while 循环也是如此。

换句话说,if 语句的工作方式如下

if ( /* Statement or condition is true */ )
// execute the code

对于你的问题

what is the difference between i++ and ++i in for loop?

考虑这个 for 循环

int i;
for ( i = 0 ; i < 5 ; i++ )
// some code

这个循环相当于

int i;
i = 0;
while ( i < 5 )
{
// some code
i++;
}

如果您的 for 循环包含 ++i 而不是 i++,那就是

int i;
for ( i = 0 ; i < 5 ; ++i )
// some code

那么它就相当于

int i;
i = 0;
while ( i < 5 )
{
// some code
++i;
}

因此,如您所见,i++++i 最后执行,因此简而言之,两个 for 循环的工作方式相同

关于c - 为什么在下面的代码中当 i=0 时这个条件失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29567038/

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