File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

for loop vs while loop

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What is the difference between for loop and while loop when we can get same result, why do we need second loop..What makes one loop better than the other one.


Output is:

x values is 0
x values is 1
x values is 2
y value is 0
y value is 1
y value is 2
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For loop is better for iterating over indexed items like arrays...
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can get the same functionality with while loop also..I may have to change the question. Which feature one loop has while the other loop doesn't have it. What I am trying to get is, Do we miss anything if we get rid of one of the loops from Java ?
 
Ram Narayan.M
Ranch Hand
Posts: 247
Chrome Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seconding Ankit's...

For loop is more suitable for iterating over arrays... If you use for loop for iteration of arrays, it will be catchy and understandable rather than while loop.

And While loop can be used in the context on which a loop has to be executed until certain condition is reached...

E.g.,

Reading contents from a file until EOF character is reached... Here while loop is apt one...
 
Ankit Garg
Sheriff
Posts: 9497
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no missing functionality in any of the loops. Its just that using for loop for iterating index elements is easier. Using while loop for the same functionality is possible but the code is ugly and harder to understand for anyone...
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to make a code that is more understandable for future developer who see your code and be thankful to you then use for loop
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64186
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:if you want to make a code that is more understandable for future developer who see your code and be thankful to you then use for loop

Not correct. Let me fix it:

"If you want to write code that is more understandable for both yourself and for future developers who see your code and be thankful to you, then use whichever loop type makes the most sense in the context of the code."

Sometimes the for loop will be the most readable, sometimes it will be the while loop. Use whechever makes the most sense -- but that's certainly not always the for loop.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for correcting...
But what i mean is for Array elements only...
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64186
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:But what i mean is for Array elements only...

Agreed, that is one of the purposes for which a for loop would most likely be the most appropriate choice.
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks..........
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you want to keep on running a loop, until certain condition meet . then while loop is my choice rather than for loop.

For an example:
 
Mina Daoud
Ranch Hand
Posts: 88
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use for loop if you know how many iterations you will need. Use while loop if you don't know how many iterations are required!
 
Piotr Nowicki
Ranch Hand
Posts: 610
IntelliJ IDE Java Linux
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For loops should be generally prefered over while, as they are easier to understand and more intuitive.

AFAIR, Bruce Eckel suggested that the for loop should be also prefered because of the granularity of the variables. For loop lets you declare, define and use iteration variable only within the for, which conforms the choose-smallest-available-scope for the variable rule.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic