When you have a choice between using either a for loop or a while loop...

When you have a choice between using either a for loop or a while loop, is it generally better to use a for loop? If so, what would the exceptions be to use a while loop?

04-03-2013, 08:20 AM

wsaryada

Re: When you have a choice between using either a for loop or a while loop...

Basically you use a for loop when you know how many time you want to do the iteration. For example you want to iterate from 1 to 10.

A while loop can be use when the number iteration is unknown. Like read all the lines from a file where the number of lines is unknown at the beginning of the loop. So you just read it until it reach the end of the file.

04-03-2013, 09:19 AM

psx2514

Re: When you have a choice between using either a for loop or a while loop...

Quote:

Originally Posted by wsaryada

Basically you use a for loop when you know how many time you want to do the iteration. For example you want to iterate from 1 to 10.

A while loop can be use when the number iteration is unknown. Like read all the lines from a file where the number of lines is unknown at the beginning of the loop. So you just read it until it reach the end of the file.

Yeah, but there are times when you can use either. In those instances, which one is generally better to use?

04-03-2013, 11:26 AM

wsaryada

Re: When you have a choice between using either a for loop or a while loop...

For example if you want to iterate from 1 to 10. Using a while loop it will be like:

Code:

int i = 1;
while (i <= 10) {
i++;
}

Using a for loop it will be:

Code:

for (int i = 1; i <= 10; i++) {
}

The while loop require you to create a variable as a counter. In this case the i variable. And this variable will available after the loop while loop is finish. But if you are using the for loop the i variable will be scoped only in the for loop. When the for loop finish this variable will be gone. And the for loop in this case look simpler. Beside that I think the both operate just the same. For loop can be said as a special kind of while loop.

04-03-2013, 11:33 AM

Tolls

Re: When you have a choice between using either a for loop or a while loop...

Quote:

Originally Posted by psx2514

Yeah, but there are times when you can use either. In those instances, which one is generally better to use?

Maybe you have an example in mind?

04-03-2013, 04:48 PM

Steve11235

Re: When you have a choice between using either a for loop or a while loop...

My opinion: Use a for loop when you want to iterate; otherwise, use a while loop. Technically, you could write an entire loop, logic and all, inside a for statement. That doesn't mean you should. Most business coders avoid writing hard-to-read code, but I have come across messy constructs inside a for loop. It doesn't matter until you are looking at someone else's code to either find a bug or make an enhancement. Then, you have to decipher exactly what their code is doing or risk missing a problem or creating one.

In the same way, I avoid writing complicated conditions in while statements. Many disagree with me, but I prefer while(true) { if (...) break; ... } constructs, just because they are more readable.

04-03-2013, 06:37 PM

Dark

Re: When you have a choice between using either a for loop or a while loop...

I generally use for loops for simple loops or loops with known iterations, like going through an array. I use while loops for unknowns, reading through unknowns such as when I'm grabbing information from something. I don't always know what is going to return, so my while statement will go until the file ends. Something that would either be difficult or look messy inside a for loop.

04-03-2013, 09:26 PM

jim829

Re: When you have a choice between using either a for loop or a while loop...

I tend to use for loops for looping a known number of times or for indexing an array or List. I tend to use while loops with iterators or when the loop condition is manifested within the loop as opposed to being known before hand. However it may boil down to personal preference. I have seen both the following for an infinite loop: