when to use while loop rather than for loop

2020-02-01 02:32发布

I am learning java as well android. Almost everything that we can perform by while loop those things we can do in for loop.

I found a simple condition where using while loop is better than for loop

if i have to use the value of counter in my program then i think while loop is better than for loop

Using while loop

int counter = 0;
while (counter < 10) {
    //do some task
    if(some condition){
        break;
    }
}
useTheCounter(counter); // method which use that value of counter do some other task

In this case I found while loop is better than for loop because if i want to achieve the same in for loop i have to assign the value of counter to another variable.

But is there any specific situation when while loop is better than for loop

13条回答
Anthone
2楼-- · 2020-02-01 02:42

Remember,

Everything done with a for loop can be done with a while loop, BUT not all while loops can be implemented with a for loop.

WHILE :

While-loops are used when the exiting condition has nothing to do with the number of loops or a control variable

FOR :

for-loops are just a short-cut way for writing a while loop, while an initialization statement, control statement (when to stop), and a iteration statement (what to do with the controlling factor after each iteration).

For e.g,

Basically for loops are just short hand for while loops, any for loop can be converted from:

for([initialize]; [control statement]; [iteration]) {
   // ...
  }

and

[initialize]; 
while([control statement]) { 
   //Do something [iteration];
   } 

are same.

查看更多
劫难
3楼-- · 2020-02-01 02:46

for and while are equivalent, just a different syntax for the same thing.


You can transform this

while( condition ) {
   statement;
}

to this:

for( ; condition ; ) {
    statement;
}

The other way:

for( init; condition; update) {
    statement;
}

is equivalent to this:

init;
while(condition) {
    statement;
    update;
}

So, just use which looks better, or is easier to speak.

查看更多
萌系小妹纸
4楼-- · 2020-02-01 02:46

What ever you can write in for loop can be converted to while loop. Benefits of using for loop are

  1. The syntax allows you to start a counter, set the condition to exit loop, then auto increment.

You can get the same done in while loop also. But all which you can do in while loop is not possible to do in for loop. For example if you have more than one counter and you want any of them to increment based on a condition then while only can use. In for loop at the end of loop the counter increment happens automatically.

Best use

For matrix or single array single directional traversal, for loop is good In case of multiple conditions and multiple counters then while loop is good. if yuo want to traverse an array from both sides based on different conditions then while loop is good.

While loop, there is lot more chance to forget increment counter and ends up into infinite loop, while in for loop syntax will help you to easily set the counter.

查看更多
聊天终结者
5楼-- · 2020-02-01 02:50

A for loop is just a special kind of while loop, which happens to deal with incrementing a variable. You can emulate a for loop with a while loop in any language. It's just syntactic sugar (except python where for is actually foreach). So no, there is no specific situation where one is better than the other (although for readability reasons you should prefer a for loop when you're doing simple incremental loops since most people can easily tell what's going on).

For can behave like while:

while(true)
{
}

for(;;)
{
}

And while can behave like for:

int x = 0;
while(x < 10)
{
    x++;
}

for(x = 0; x < 10; x++)
{
}

In your case, yes you could re-write it as a for loop like this:

int counter; // need to declare it here so useTheCounter can see it

for(counter = 0; counter < 10 && !some_condition; )
{
    //do some task
}

useTheCounter(counter);
查看更多
相关推荐>>
6楼-- · 2020-02-01 02:52

Use a FOR loop when you know the number of times you want to loop. The technical term for that is the number of iterations. How do you know the number of iterations? You know the start, stop and step. If you know those three pieces of information, you should use a FOR loop because it's the right tool for the job.
Use a DO loop when you don't know the number of iterations. If you don't know the start, stop, step or some combination of those then you need to use a DO loop. The expression will be evaluated at the top of the loop. Use a DO WHILE loop if you want to loop at least once. Use just a WHILE loop if you don't want to loop at least once. The expression will be evaluated at the bottom of the loop.

查看更多
一纸荒年 Trace。
7楼-- · 2020-02-01 02:53
int counter = 0;
while (counter < 10) {
    //do some task
    if(some condition){
        break;
    }
}
useTheCounter(counter); // method which use that value of counter do some other task

Hi I repeat your code because it is incorrect. You forget to increase your counter so it will remains on 0

int counter = 0;
while (counter < 10) {
  //do some task
  if(some condition){
      break;
  }
  counter++;
}
useTheCounter(counter); // method which use that value of counter do some other task
查看更多
登录 后发表回答