循环直到条件为真 [英] Loop until condition is true

查看:108
本文介绍了循环直到条件为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


总有一个好的用Python做事的方法,但这次我不能给b $ b找到一个。


我想要实现的是一个条件循环条件

测试将在最后完成,因此循环至少执行一次。这是&b
与while相反的方式。


到目前为止,我得到的只是:


而True:

一些(代码)

如果final_condition为True:

break






我没有找到的所以很好是必须建立一个无限循环只有

打破它。


有更好的食谱吗?


-

==================

Remi Villatel
maxilys _ @ _ tele2.fr

==================

解决方案

Quoth Remi Villatel< maxilys@SPAMCOP_tele2.fr>:


|我想要实现的是一个条件循环,条件是

|测试将在最后完成,因此循环至少执行一次。这是

|某种方式与while相反。

|

|到目前为止,我得到的只是:

|

|而真:

|一些(代码)

|如果final_condition为True:

|打破

| #

| #

|

|我发现的并不是那么好看是必须建立一个无限循环只有

|打破它。

|

|有更好的食谱吗?


这取决于,但这并不是太糟糕。替代方案是标志

变量,如looping = 1; while循环:..."


你正在寻找的构造是直到在C中,已经有很多建议将这个或其他改进添加到Python的

保留曲目中。据我所知,它并没有发生,因为它不是真正需要的b $ b。如果你看看C代码,至少根据我的经验,

"直到&循环很少使用。 (例如,我没有在源代码中看到过一次对Python 2.4的
。)同时,while True (或while 1)

idiom对Python程序员来说非常熟悉(就像Python源代码那样
有几十个for(;;)) ,所以它是易读的首选。这很好,

别担心。


Donn Cave,做** @ drizzle.com


Donn Cave写道:

如果*你*看* * * * * * * * * * * * * * * * * * * * * * * * * * *循环很少使用。**(例如,我*不* * * * * * * * * * * * * * * * / / / / / / / / / / >
很久没有C?


''直到''在C中实际上是


do

声明

while(表达);


我发现136次出现do {对于754的while和1224的对于

(在Python 2.4源代码中,所以使用这些粗略的估计值仍然是

符合条件作为很少使用。


彼得


2005-06-18,Peter Otten< __*******@web.de>写道:

如果*你*看* * C *代码,* at *至少* in * my * experience * t
直到循环很少使用。**(我*不* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *比如Python 2.4。)



很久没有C?

''直到''在C中实际上是


声明
while(表达式);

我发现136次出现do {与754 ofwhile(和1224对于
(在Python 2.4源代码中,所以使用这些粗略的估计值仍然可以确定为很少使用。




AFAI CT,C中do / while的主要用途是当你想要
定义一个带有局部变量作为宏的代码块时:


#define DoSomething(foo)\

do \

{\

int i; \\ /

/ *用foo和i * / \

}做点什么,而(0)


-

格兰特爱德华兹格兰特哇!我在这里劫持了747到

!我希望那些

visi.com神话般的CONEHEADS在家里是

!!


Hi there,

There is always a "nice" way to do things in Python but this time I can''t
find one.

What I''m trying to achieve is a conditionnal loop of which the condition
test would be done at the end so the loop is executed at least once. It''s
some way the opposite of "while".

So far, all I got is:

while True:
some(code)
if final_condition is True:
break
#
#

What I don''t find so "nice" is to have to build an infinite loop only to
break it.

Is there a better recipe?

--
==================
Remi Villatel
maxilys_@_tele2.fr
==================

解决方案

Quoth Remi Villatel <maxilys@SPAMCOP_tele2.fr>:

| What I''m trying to achieve is a conditionnal loop of which the condition
| test would be done at the end so the loop is executed at least once. It''s
| some way the opposite of "while".
|
| So far, all I got is:
|
| while True:
| some(code)
| if final_condition is True:
| break
| #
| #
|
| What I don''t find so "nice" is to have to build an infinite loop only to
| break it.
|
| Is there a better recipe?

It depends, but that isn''t too bad. The alternative would be flag
variable, like "looping = 1; while looping: ..."

The construct you''re looking for is "until" in C, and there have been
plenty of proposals to add this or other improvements to Python''s
repertoire. As far as I know, it hasn''t happened because it isn''t
really needed. If you look at C code, at least in my experience the
"until" loop is quite rarely used. (I don''t see it once in the source
to Python 2.4, for example.) Meanwhile, the "while True" (or "while 1")
idiom is very familiar to Python programmers (just as the Python source
has dozens of "for (;;)"), so it''s preferred for legibility. It''s nice,
don''t worry.

Donn Cave, do**@drizzle.com


Donn Cave wrote:

If*you*look*at*C*code,*at*least*in*my*experience*t he
"until" loop is quite rarely used.**(I*don''t*see*it*once*in*the*source
to Python 2.4, for example.)



Long time no C?

''until'' in C is actually

do
statement
while (expression);

I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of "for
(" in the Python 2.4 source, so using these rough estimates do-while still
qualifies as "rarely used".

Peter


On 2005-06-18, Peter Otten <__*******@web.de> wrote:

If*you*look*at*C*code,*at*least*in*my*experience*t he
"until" loop is quite rarely used.**(I*don''t*see*it*once*in*the*source
to Python 2.4, for example.)



Long time no C?

''until'' in C is actually

do
statement
while (expression);

I found 136 occurrences of "do {" versus 754 of "while (" and 1224 of "for
(" in the Python 2.4 source, so using these rough estimates do-while still
qualifies as "rarely used".



AFAICT, the main use for do/while in C is when you want to
define a block of code with local variables as a macro:

#define DoSomething(foo) \
do \
{ \
int i; \
/* do something with foo and i */ \
} while (0)

--
Grant Edwards grante Yow! I HIJACKED a 747 to
at get here!! I hope those
visi.com fabulous CONEHEADS are
at HOME!!


这篇关于循环直到条件为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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