-1

Some iteration code is written like this

while(true)                           for(int i = 0 ; i<=4;++i)
                                        {
{  
    System.out.println(i);               System.out.println(i);
   if(i==4)                                
     break;
                                          }
}

both have the same logic inside it take it as assumption. So which one will be faster, better to use in code and what are the implication of using any one of it??

Deep Saxena
  • 205
  • 3
  • 5

8 Answers8

1

The first one is an endless loop :) (depending on the i before the loop) The second one does (almost) what you expect. Go grab a good manual about your programming language, it will explain the difference.

On another level: Loop optimizations are much better suited to for loops (AFAIK) so a "clever" compiler might generate better code for the "for" loop. http://en.wikipedia.org/wiki/Loop_optimization

Ferenc Deak
  • 34,348
  • 17
  • 99
  • 167
0

They are totally different, but if their behavior was the same, never use break; in such situations.

The second bucle is more clear, when you have to take into consideration performance, you don't have to do it in retreat of clean code.

The second bucle is self-documented while the first you need to add a reason to add the break in order to be understandable.

RamonBoza
  • 8,898
  • 6
  • 36
  • 48
0

to end the while loop on the left you have to incriment i

EDIT : Assuming i is less that 4 when the loop starts

sukhvir
  • 5,265
  • 6
  • 41
  • 43
0

For that purpose, use a for one, since you need the value of i. It is more understanding for maintenance. Anyway, the compiler will optimize both codes and runtime may be the same.

Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87
0

Considering your while is not an infinite loop there is no difference as far as performance is concerned. However, I personally prefer for loop as it is cleaner. Consider this:

int i = 0;
while (true) {
  doSomething(i++);
  if (i == 4)
    break;
}

which looks cleaner with

for (int i = 0; i <= 4; i++)
  doSomething(i);
nomann
  • 2,257
  • 2
  • 21
  • 24
0

Im using for whenever I know the number of itterations in advance.

FeinesFabi
  • 1,147
  • 2
  • 12
  • 25
0

If you know how many time you want to do some operation you have to use for loop otherwise use while loop. For example:

  • If you have some array or arraylist then suppose you want to add all elemnts of it then you should use for loop because you know size of arraylist by size() function and know the size of array by length attribute.
  • If you have resultset and you want to retrieve all rows from it then you should use while loop because you don't know how many element in resultset object.
Meet
  • 242
  • 1
  • 4
  • 13
0

Neglecting the typo of your question,Assuming that you want to know which one i choose when Both loops are essentially doing the same thing.

The answer is you can go for any one.

But it's better to use for loop if you know how many times you want to run a loop as you can set the condition during initialization. And in future also it will be easy to change it if you want to extend the iteration.

You can prefer while if you are not sure for how much iteration you want. In your case i will go with for loop

Note: the answer is opinion based.

Arpit
  • 12,767
  • 3
  • 27
  • 40