while()和测试条件 [英] while( ) and test condition

查看:107
本文介绍了while()和测试条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果i> 0,则执行while循环;如果i == 0没有。


好​​的,但是如果i< 0则执行while循环。


所以,这是规则是什么?哪些值必须假定测试条件

假定为真假?提前致谢

#include< stdio.h>


int main(无效)

{


int i = 0; / * while循环没有被执行* /

而(i)


{

printf(" i' 'while-looping ... Ctrl-C退出');

}

}

If i>0 the while loop is executed; if i==0 not.

Ok, but also if i<0 the while loop is executed.

So, which are the rules? Which values must assume the "test condition"
to be assumed like true o false? Thanks in advance
#include <stdio.h>

int main( void )
{

int i = 0; /* while loop isn''t executed */
while (i)

{
printf ("i''m while-looping...Ctrl-C to exit");
}
}

推荐答案

nembo kid写道:
nembo kid wrote:

如果i> 0,则执行while循环;如果i == 0没有。


好​​的,但是如果i< 0则执行while循环。


所以,这是规则是什么?哪些值必须假定测试条件

假定为真假?提前致谢


#include< stdio.h>


int main(无效)

{


int i = 0; / * while循环没有被执行* /


而(i)


{

printf( 我在循环时... Ctrl-C退出);

}


}
If i>0 the while loop is executed; if i==0 not.

Ok, but also if i<0 the while loop is executed.

So, which are the rules? Which values must assume the "test condition"
to be assumed like true o false? Thanks in advance
#include <stdio.h>

int main( void )
{

int i = 0; /* while loop isn''t executed */
while (i)

{
printf ("i''m while-looping...Ctrl-C to exit");
}
}



而(i)



完全相同,而((i)!= 0)

因此:

而(i> 0)



完全相同((i> 0) != 0)

和:

而(i <0)

意味着与

完全相同while((i< 0)!= 0)


-

pete

while (i)
means the exact same thing as
while ((i) != 0)

Therefore:
while (i>0)
means the exact same thing as
while ((i>0) != 0)
and:
while (i<0)
means the exact same thing as
while ((i<0) != 0)

--
pete


nembo小孩< nembo @ kidwrote:
nembo kid <nembo@kidwrote:

那么,规则是什么?哪些值必须假定测试条件

假定为真假?在C中预先感谢
So, which are the rules? Which values must assume the "test condition"
to be assumed like true o false? Thanks in advance



_All_逻辑条件,无论它们是否有一段时间

循环,一个for循环,一个如果声明或任何地方,如果它们比较等于0,则为假,如果它们等于0,则在所有其他情况下为真。

(注意:比较等于0不一定意味着是一个整数,其值为

zero。例如,您可以将指针表达式放在if语句中,

如果指针不是a,它将触发null指针。)


Richard

_All_ logical conditions in C, regardless of whether they''re in a while
loop, a for loop, an if statement, or wherever, are false if they
compare equal to 0, and true in all other cases.
(Note: compare equal to 0 need not mean "is an integer and has the value
zero". For example, you can put a pointer expression in an if statement,
and it will trigger if the pointer is not a null pointer.)

Richard


nembo kid写道:
nembo kid wrote:

如果i> 0,则执行while循环;如果i == 0没有。


好​​的,但是如果i< 0则执行while循环。


所以,这是规则是什么?哪些值必须假定测试条件

假定为真假?提前致谢


#include< stdio.h>


int main(无效)

{


int i = 0; / * while循环没有被执行* /


而(i)


{

printf( 我在循环时... Ctrl-C退出);

}


}
If i>0 the while loop is executed; if i==0 not.

Ok, but also if i<0 the while loop is executed.

So, which are the rules? Which values must assume the "test condition"
to be assumed like true o false? Thanks in advance
#include <stdio.h>

int main( void )
{

int i = 0; /* while loop isn''t executed */
while (i)

{
printf ("i''m while-looping...Ctrl-C to exit");
}
}



正如其他人已经说过的那样,零是假的,所有其他值都是真实的。无论如何,为了清楚起见,最好是明确地做一个

比较(除非变量或函数有一个布尔解释

) - 我们想专注于真正的问题,而不是技巧
语言中的


八月

As others have already said, zero is false and all other values are
true. Anyway, for clarity it is probably best to be explicit and do a
comparison (unless a variable or function has a boolean interpretation
already) -- we want to concentrate on real problems, not on "tricks"
within the language.
August


这篇关于while()和测试条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆