关于for循环的问题 [英] A question on for loop

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

问题描述

这两个版本的代码会有效吗?假设所有变量都已初始化。我认为一个更好的问题可能是两个版本的效果有所不同吗?


版本1:

for(; i< R; i ++){...}


版本2:

for(; i< R; ++ i){...}

Would there be any difference in effect in these two versions of code? Just assume that all variables are initialized. I think a better question might be is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }

推荐答案

文章< 87 ************ @ student.uts.edu.au>,

Logan Lee< Lo ********* @ student.uts.edu.auwrote:
In article <87************@student.uts.edu.au>,
Logan Lee <Lo*********@student.uts.edu.auwrote:

>会不会有任何区别这两个版本的代码效果如何?只是
假设所有变量都已初始化。我认为一个更好的问题可能是两个版本的效果有何不同?

版本1:
(; i< R; i ++){。 ..}

版本2:
for(; i< R; ++ i){...}
>Would there be any difference in effect in these two versions of code? Just
assume that all variables are initialized. I think a better question might be
is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }



不,他们是一样的。


- Richard

-

:wq

No, they''re the same.

-- Richard
--
:wq


Logan Lee< Lo ********* @ student.uts.edu.auwrites:
Logan Lee <Lo*********@student.uts.edu.auwrites:

这两个版本的代码在效果上有什么不同?

假设所有变量都已初始化。我认为更好的问题可能是两个版本

的效果有什么不同?


版本1: for(; i< R; i ++){...}


VERSION 2:for(; i< R; ++ i){...}
Would there be any difference in effect in these two versions of code?
Just assume that all variables are initialized. I think a better
question might be is there a case where the effects of two versions
differ?

VERSION 1: for(; i < R; i++) { ... }

VERSION 2: for(; i < R; ++i) { ... }



这些循环是等效的。此外,对于许多(大多数?)

编译器,生成的代码将是相同的。

-

Ben Pfaff
< a rel =nofollowhref =http://benpfaff.orgtarget =_ blank> http://benpfaff.org


On星期二,2007年12月18日09:09:09 +1100,Logan Lee

< Lo ********* @ student.uts.edu.auwrote:
On Tue, 18 Dec 2007 09:09:09 +1100, Logan Lee
<Lo*********@student.uts.edu.auwrote:

>这两个版本的代码在效果上会有什么不同吗?假设所有变量都已初始化。我认为一个更好的问题可能是两个版本的效果有所不同吗?

版本1:
((i< R; i ++){...} <版本2:
for(; i< R; ++ i){...}
>Would there be any difference in effect in these two versions of code? Just assume that all variables are initialized. I think a better question might be is there a case where the effects of two versions differ?

VERSION 1:
for(; i < R; i++) { ... }

VERSION 2:
for(; i < R; ++i) { ... }



首先,写作一个for循环,然后没有初始化变量i在

开头是一个可憎的做法。 for的全部意义。就是那个

3个控制元素被放置在循环的顶部,所以它确实是清楚发生了什么。如果我必须从那里找到

,那么i的初始值应该是什么,这使得代码更难以阅读和维护。我不知道这种做法出现在哪里

但不管它在哪里都应该被劝阻。如果你打算

取决于我在其他地方进行初始化,你可以将

写成循环作为while结构。


至于V1与V2,它们是相同的。

First, writing a for loop and then not initializing variable i in the
beginning is a detestable practice. The whole point of "for" is that
the 3 control elements are placed at the top of the loop so it is
clear exactly what is going on. If I have to go up from there to find
out what the initial value of i is supposed to be it makes the code
harder to read and maintain. I don''t know where this practice arose
but it should be discouraged wherever it is found. If you are going to
depend on i being initialized somewhere else, you may as well have
written the loop as a while construct.

As for V1 vs. V2, they are the same.


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

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