这就跟你问声好!
我需要帮助我的头脑如何在冷静的while循环工作。的文档很不幸,一点用都没有。: /
我不明白的第一件事就是而循环有固定的迭代次数。这对我来说是为循环。一个而循环在条件不满足时运行。
第二件让人困惑的事情是,我可以将退出条件设置为成功、失败或不在乎。我想《不在乎》会让而循环到实际的为循环?
成功或失败的条件是我能够完全判断出来的,因为这是基于循环中任务的执行结果,而不是我所控制的参数。如果我选择“成功”作为退出条件,我是否必须故意让任务失败才能继续循环?
如果循环中有多个任务,退出条件触发时,所有任务都需要失败或成功吗?
是否有可能在我的脚本中设置一个参数,然后使用它作为退出条件?
谢谢。
最佳答案Srinathgs.
\u00a0<\/p>
In most programming languages, While & For loop are usually convertible. If a variable initialisation, break condition and variable update mechanisms are given together, it constitutes a For loop and if just the break condition is present in the loop, it is a While loop.\u00a0<\/p>
\u00a0<\/p>
But, our implementation is inspired from a pythonic way of defining this, where For loop iterates over an iterable, such as a list, set, or a dictionary, whereas a While loop contains a condition.\u00a0<\/p>
\u00a0<\/p>
You can consider our While loop to be of the following implementation.<\/p>
\u00a0<\/p>
can_continue = true<\/span><\/span><\/p> \u00a0<\/p> here, the \u00a0<\/p> Following is an example escript where I achieved the same.\u00a0<\/p> \u00a0<\/p>iteration = 0<\/code><\/p>
max_iteration = n<\/code><\/p>
while (iteration < max_iteration) or can_continue:<\/code><\/p>
\u00a0 \u00a0 can_continue = loop_body()<\/code><\/p>
\u00a0 \u00a0 iteration += 1<\/code><\/p>
can_continue<\/code> can be manipulated by the task that the loop is executing and currently, can_continue can be manipulated by the status of the loop_body as you\u2019ve found out.\u00a0\u00a0<\/p>