Best Practice In Java

Dear Friends. Which is the best method to use in looping ? For loop or While lopp. Please give me some idea about the performance.Thanks in advanceShaiju.P
[183 byte] By [Shaiju_Nair] at [2007-9-30 18:17:14]
# 1

The choice between for or while is pretty much a matter of readability and personal preference. I personally tend to use for loops only for dealing with situations where a numerical index is important (arrays and sometimes lists). For iterators, I tend to use while() loops, because it looks more natural to me.

I don't think there's any difference in performance pers? but you always need to be aware of when and how many times your condition gets evaluated.

Herko_ter_Horst at 2007-7-6 19:26:13 > top of Java-index,Administration Tools,Sun Connection...
# 2
Performance is VERY unlikely to be an issue.My only suggestion is don't have too many nested loops.
Peter-Lawrey at 2007-7-6 19:26:13 > top of Java-index,Administration Tools,Sun Connection...
# 3
hello,Very hard question...danny http://www.myinternetinfo.com
gaiisin at 2007-7-6 19:26:13 > top of Java-index,Administration Tools,Sun Connection...
# 4

Even though it is a personal choice, I would have made the decision based on the following:

I would have used 'for' loop if i know exactly how much times i need to loop, say i need loop 30 times.

But if i don't know when a condition would be met, I would prefer to use a 'while' loop.

Kumar

kumssan at 2007-7-6 19:26:13 > top of Java-index,Administration Tools,Sun Connection...
# 5
Before making the .class file of your .java file, the java compiler does a code optimization. In this step, all the loops are treated in a similar manner. It uses jumps to implement them.So, performance is not at all an issue.
kapilChhabra at 2007-7-6 19:26:13 > top of Java-index,Administration Tools,Sun Connection...