When to use a while loop rather than a loop

I am learning java as well as android. Almost everything we can do, while a loop we can do in a loop.

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

if I need to use the counter value in my program, then I think the loop is better than for the loop

Using a 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 that the loop is better than for the loop, because if I want to achieve the same thing in the for loop, I have to assign the counter value to another variable.

But is there any specific situation where the while loop is better than for the loop

+3
source share
11 answers

One of the main differences is while . Loops are best if you donโ€™t know in advance the number of iterations you need to complete. When you know this before entering the loop, you can use the for loop.

+8
source

A for loop is just a special type of while loop that deals with incrementing a variable. You can emulate a for loop with a while in any language. It is just syntactic sugar (except for python, where for is actually foreach ). Thus, no, there is no specific situation where one is better than the other (although for reasons of readability, you should prefer the for loop when you do simple incremental loops, since most people can easily tell what is happening).

For can behave like while:

 while(true) { } for(;;) { } 

And although it can behave like:

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

In your case, yes, you can rewrite 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); 
+13
source

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


You can convert this

 while( condition ) { statement; } 

:

 for( ; condition ; ) { statement; } 

Another way:

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

equivalent to this:

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

So, just use one that looks better or easier to say.

+4
source

the for is finite, in the sense that it will end the loop when the loop ends, to go through ....

while it can be infinite if the condition is not satisfied or a broken cycle

Edit

My mistake ... for can be infinite.

+2
source

The while loop is usually better if you don't have an iterator (usually a counter).

+2
source

Remember

Everything that is done with the for loop can be done using the while loop, but not all, while loops can be implemented with the for loop.

WHILE:

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

FOR:

for-loops is just a shortcut to writing a while loop, while an initialization statement, a control statement (when to stop), and an iteration statement (what to do with a control factor after each iteration).

For instance,

Basically for loops, it's just a short hand for loops, any of loops can be converted from:

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

and

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

match up.

+2
source

One thing that I think should be noted is that when using a for loop you do not need to assign a counter to another variable. For example, for(counter=0; counter<10; counter++) is valid Java code.

As for your question, the for loop is usually better if you want part of the code to run a certain number of times, and the while loop is better when the condition for the code to continue to work is more general, for example with a Boolean flag that is set only to true when certain the condition is satisfied in the code block.

+1
source

You can do something like:

 int counter; for (counter = 0; counter < 10; ) { //do some task if(some condition){ break; } } useTheCounter(counter); 

Everything that a while loop can do can also be done in a for loop, and everything that a for loop can do can also be done in a while loop.

+1
source

No. There is no specific situation where for better than while .
They do the same thing.
It is up to you when you apply one of them.

+1
source
Cycles

while much more flexible, and for loops are much more readable if that is what you are asking for. If you're wondering which one is faster, then look at this experiment that I conducted regarding the speed of the for and while loops.

https://sites.google.com/a/googlesciencefair.com/science-fair-2012-project-96b21c243a17ca64bdad77508f297eca9531a766-1333147438-57/home

Cycles

while faster.

0
source
 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 am repeating your code because it is incorrect. You will forget to increase the counter so that it remains at 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 
0
source

All Articles