开发者

What is the correct way to convert from a for loop to a while loop?

I have a for loop of the form:

for (int i = from; i < to; i++) {
  // do some code (I don't know exactly what, it is subject to change)
}

And I want to convert it to a while loop (mostly because I want to play with the value of i in开发者_如何学Pythonside the loop to go backwards and forwards and my co-worker thinks that doing this in a for loop is prone to problems. I tend to agree with him). So I wrote something like this:

int i = from;
while (i < to) {
  try {
    // do some code (I don't know exactly what, it is subject to change)
  } finally {
    i++;
  }
}

Which prompted some some loud comments. My reasoning is that you don't know what the code inside the loop does - it may (and does) have multiple continue commands.

As a response he wrote this:

int i = from - 1;
while (++i < to) {
  // do some code (I don't know exactly what, it is subject to change)
}

Granted its less lines, but I still think my code is more elegant - what do you think?


Playing with the value of your index while in a looping structure is prone to problems, no matter what the looping structure is.

It's not going to matter if it's a for loop or a while loop, the point is will the indexer eventually lead you to make a decision of loop termination?

If you're confident that you're indexer will eventually cause your exit condition to be achieved, then that is all you should be concerned with, not whether to use a for or a while.


And I want to convert it to a while loop (mostly because I want to play with the value of i inside the loop to go backwards and forwards and my co-worker thinks that doing this in a for loop is prone to problems. I tend to agree with him).

This is perfectly acceptable in most languages. There is no reason to avoid a for loop.


It seems to me that it may be easier and more readable to convert it to:

while (condition == true) {
   // do stuff
   // set the condition flag appropriately
}

and thus separate the termination of the loop from the variable incrementation.

If I see a loop with a limit check (e.g. i < limit) I would tend to assume that there's a variable that is being modified in a (reasonably) consistent fashion. There's no reason why you can't do what you want, but I would lean towards the more readable and more expected behaviour.


Why bother with silly loops when you can do the same (and much more!) with the uber-powerful goto?

i = fro;
my_loop:
//all your stuff here
i++;
if (i < to) goto my_loop;

If you are one of those faint hearted programmers that diminish the goto, then you can try with this:

i = fro;
while(1) {
    //your stuff here
    if (++i < to) break;
}


The easiest way to do this would be to not convert into a while loop, such as below.

for (int i = from; i < to; ) {
  // do some code (I don't know exactly what, it is subject to change)
  i += rand()*10;
}


To answer the question about which code I would select; I choose your longer code. Its MUCH easier to read the first(longer) loop. And yes I can read the second but even if you have lots of experience you have to look twice to know what that loop does. Plus the compiler will optimize the code well enough.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜