使用空的 for 循环有什么问题吗?

发布于 2024-11-04 21:50:32 字数 430 浏览 1 评论 0原文

自从我上次编程以来已经有一段时间了,我似乎忘记了使用空的“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

青春有你 2024-11-11 21:50:32

您正在做的事情非常好,并且是编写和退出无限循环的惯用方式。

What you're doing is perfectly fine, and an idiomatic way of writing and exiting an infinite loop.

尘曦 2024-11-11 21:50:32

我总是使用 while(true) 进行无限循环

I always use while(true) for infinite loops

千里故人稀 2024-11-11 21:50:32

我在几个地方看到过这个:

#define forever for(;;)

forever {

}

但不确定我是否会推荐它。

I've seen this in a few places:

#define forever for(;;)

forever {

}

Not sure I'd recommend it though.

飘然心甜 2024-11-11 21:50:32

for(;;)while(1) 都是可以接受的。这些只是语言提供的条件循环,您可以根据您的要求使用它们来实现无限运行循环。

for(;;) as well as while(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.

爱情眠于流年 2024-11-11 21:50:32

这是有效的,您可以继续使用您的代码。

This is valid, you can go ahead with your code.

蔚蓝源自深海 2024-11-11 21:50:32

是的,这是完全可以接受的。一旦循环中有退出条件(breakreturn),您就可以在循环语句中使循环“无限” - 您只需将退出条件从循环中移出即可语句进入循环体。如果这使程序更具可读性,您当然可以这样做。

Yes, it's totally acceptable. Once you have an exit condition (break or return) 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.

冷情妓 2024-11-11 21:50:32

对于无限循环 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 a while loop.

挽心 2024-11-11 21:50:32

您还可以使用带有条件的 while 循环来重复请求用户输入。

while (condition) {
  ...
}

您可以使用 .

You can also use while loop with condition to repeatedly request user to input.

while (condition) {
  ...
}

Instead of IF block to validation you can use the .

无远思近则忧 2024-11-11 21:50:32

您所描述的内容可以正常工作,但值得一提的是,某些严格的编码标准(即 MISRA)会不赞成在函数结束之前使用return

如果您的代码遵循此类标准,那么您可以使用带有合适退出条件的 do-while 循环:

do {
   // get userinput
   if (userinput != '0')
   {
       // do stuff 
   }
} while (userinput != '0');

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:

do {
   // get userinput
   if (userinput != '0')
   {
       // do stuff 
   }
} while (userinput != '0');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文