user2203117
user2203117

Reputation:

While loops and for loops

In general isn't the while loop just a rephrase for the for loop:

for( starting point; condition; excution code){}
//and while
starting point;
while(condition)
{excution code;
 }

I'm really sorry if this seems a dumb question. But I'm just wondering if there is a case where it is better to use while than for. Or vice versa.

Upvotes: 1

Views: 1100

Answers (4)

mxm
mxm

Reputation: 615

You're right. Every for loop can be rewritten as a while loop and vice versa. However, in terms of code readability each loop has it's pros and cons.

Typically, if you need a variable in a loop that increases until a condition is reached, you'd use a for loop.

Printing an Array

for(int i = 0; i < array.length; i++) {
    print("Element " + i + ": " + array[i]);
}

If you're just waiting for a condition, you'd prefer a while loop:

Reading from a stream

while((String line = buffer.readLine()) != null) {
    print("Next line: " + line);
}

Although equal in terms of semantic, I wouldn't want to see those loops:

Printing an Array

int i = 0;
while(i < array.length) {
    print("Element " + i + ": " + array[i]);
    i++;
}

Reading from a stream

for(String line = buffer.readLine(); line != null; line = buffer.readLine()) {
    print("Next line: " + line);
} 

Upvotes: 1

Rahul Tripathi
Rahul Tripathi

Reputation: 172398

No that is not the case. Both the loops have different uses. While loops, in general, are used when we have an indefinite number of iterations and for loops are used when we have a more definite number of iterations. However you can convert one to the other(although I would not recommend that)

Also one important aspect is that in a for loop the scope of the iterator is set to just inside for loop.

But I'm just wondering if there is a case where it is better to use while than for.

I would prefer or choose for over while as it is more readable(although some may argue that the latter is more readable.) And also because of the iterator scope reason.

Also as far as similarity is concerned while loop is similar to:

while (condition) 
{
   statements;
}

is similar to

if (condition) 
{
   do 
   {
      statements;
   } while (condition);
}

Also you can use while when you dont want to use any counter and your sole motive is for the completion of the conditon.

Upvotes: 1

Ingo
Ingo

Reputation: 36329

The intention is to use a for loop for a fixed number of iterations, and the while loop for an indefinite number of iterations.

In fact, one can write each for loop as a while loop with prior initialization, as you supected. (But the converse is also true in most languages.)

Personally, I tend to use while loops, unless I am iterating over an array or string, where the size if known.

Note also that, in Java, there is a form of a for loop that looks like:

 for (E elem : collection) { ... }

that goes through an iterable collection of elements of type E. This is a very nice thing, and very intuitive, and, it keeps you from committing off by one or re-initialization errors.

Upvotes: 0

thebjorn
thebjorn

Reputation: 27311

All for loops can be rewritten as while loops, and vice-versa. The difference is in programmer intent: are you repeating something while a condition is true? ..or are you iterating from something to something?

Upvotes: 2

Related Questions