使用空的 for 循环有什么问题吗?
自从我上次编程以来已经有一段时间了,我似乎忘记了使用空的“for 循环”来创建无限循环是否可以接受?
for(;;)
目前,我在程序中使用这种方法,使其反复要求用户输入两个数值,每个数值对应程序中的每个双精度变量。然后程序调用一个函数并计算这两对数字的和。
为了终止程序,我有“if”语句来检查用户输入值是否为零,如果该值为零,则程序使用“Return 0;”终止。争论。
该程序在将值分配给变量后立即检查每个用户输入值是否为零。
所以真正的问题是:这是使我的程序执行我所描述的操作的正确方法吗?或者有更多/更好/可接受的编程方式吗?
其次,像我在这个程序中使用的方式使用“Return 0”参数有什么问题吗?
如果您认为很难理解我所写的内容或意思,请回复,我会花更多时间来写所有内容。
It was a little while since I last programmed and I have seem to forgotten if it's acceptable to use an empty "for loop" for creating an infinite loop?
for(;;)
Currently I use this method in a program to make it repeatedly ask the user to enter two numeric values one for each double variable in the program. The programs then calls a function and calculates a sum of these two pairs of numbers.
To terminate the program i have "if" statements that check if the user input value is zero, If the value is zero the program terminates using an "Return 0;" argument.
The program checks each user input value if it's zero directly after the value has been assigned to the variable.
So to the real question: Is this a correct way to make my program do what i described? Or is there a more/better/accepted way of programming this?
And secondly is there anything wrong with use the "Return 0" argument the way i did in this program?
If you thinks it's hard to understand what I'll wrote or meant please reply, and I will take more time to write everything.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您正在做的事情非常好,并且是编写和退出无限循环的惯用方式。
What you're doing is perfectly fine, and an idiomatic way of writing and exiting an infinite loop.
我总是使用
while(true)
进行无限循环I always use
while(true)
for infinite loops我在几个地方看到过这个:
但不确定我是否会推荐它。
I've seen this in a few places:
Not sure I'd recommend it though.
for(;;)
和while(1)
都是可以接受的。这些只是语言提供的条件循环,您可以根据您的要求使用它们来实现无限运行循环。for(;;)
as well aswhile(1)
both are acceptable. These are just conditional loops provided by the language and you can use them to have a infinite running loop as per your requirement.这是有效的,您可以继续使用您的代码。
This is valid, you can go ahead with your code.
是的,这是完全可以接受的。一旦循环中有退出条件(
break
或return
),您就可以在循环语句中使循环“无限” - 您只需将退出条件从循环中移出即可语句进入循环体。如果这使程序更具可读性,您当然可以这样做。Yes, it's totally acceptable. Once you have an exit condition (
break
orreturn
) in a loop you can make the loop "infinite" in the loop statement - you just move the exit condition from the loop statement into the loop body. If that makes the program more readable you of course can do that.对于无限循环
for (;;)
是相当常见的做法。但如果您确实有一个条件,例如非零用户输入,您始终可以在while
循环中完成该检查。For an infinte loop
for (;;)
is fairly common practice. But if you do have a condition, such a non-zero user input, you could always have that check done in awhile
loop.您还可以使用带有条件的 while 循环来重复请求用户输入。
您可以使用 .
You can also use while loop with condition to repeatedly request user to input.
Instead of IF block to validation you can use the .
您所描述的内容可以正常工作,但值得一提的是,某些严格的编码标准(即 MISRA)会不赞成在函数结束之前使用
return
。如果您的代码遵循此类标准,那么您可以使用带有合适退出条件的 do-while 循环:
What you describe will work fine, but it is worth mentioning that certain strict coding standards (i.e. MISRA) would disapprove of using a
return
before the end of a function.If your code is subject to such standards then you could use
do-while
loop with a suitable exit condition instead: