等效于使用for循环而不是do-while循环 [英] Equivalent using for-loop instead do-while-loop
问题描述
我想知道不是使用do-while循环,而是c中等效的for循环或任何其他循环组合?
任何种类的循环都可以通过无限的永远"循环和条件 break
语句的组合来构造./p>
例如,进行转换
do {< action>} while(< condition>);;
进入 for
循环,您可以执行以下操作:
for(;;){< action>如果(!< condition>)中断;}
您可以使用相同的技巧将 do
/ while
循环转换为 while
循环,如下所示:
while(true){< action>如果(!< condition>)中断;}
此外,根本不需要循环:可以用顶部的标签和底部的 goto
来建模以上任何内容;这是在汇编语言中执行循环的常见方式.最初将三种循环结构引入语言的唯一原因是使语言更具表现力: do
/ while
循环比其他方法更能体现作者的思想以上显示的替代方案.
I was wondering instead of using a do-while loop, what is the equivalent for-loop or any other combination of loops in c?
Any sort of a loop can be constructed from a combination of an infinite "forever" loop, and a conditional break
statement.
For example, to convert
do {
<action>
} while (<condition>);
to a for
loop, you can do this:
for (;;) {
<action>
if (!<condition>) break;
}
You can use the same trick to convert a do
/while
loop to a while
loop, like this:
while (true) {
<action>
if (!<condition>) break;
}
Moreover, the loop is not needed at all: any of the above can be modeled with a label at the top and a goto
at the bottom; that is a common way of doing loops in assembly languages. The only reason the three looping constructs were introduced into the language in the first place was to make the language more expressive: the do
/while
loop conducts author's idea much better than any of the alternatives shown above.
这篇关于等效于使用for循环而不是do-while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!