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

What's the difference between looping statements?

 
Robel Bayisa
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

So a couple of months ago, while just discovering the Java language and coding with different functions and whatnot, our teacher introduced the "while" & "for" loops. Of course it seemed pretty simple to understand what these functions did at first but the teacher said that there are a few subtleties that differentiate the two functions.

I remember coding a simple program very recently, and at a certain line, I used the "while" loop so that I could make the program repeat a few operations. So when I ran the program it didn't work... I then replaced the "while" loop with the "for" loop while respecting the coding form, and it worked like magic.

This got me a bit confused, so I was wondering if someone could tell me what the difference between "while" and "for" is?
 
Winston Gutkowski
Bartender
Pie
Posts: 9484
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Robel Bayisa wrote:This got me a bit confused, so I was wondering if someone could tell me what the difference between "while" and "for" is?

Quite a lot. You should probably read Java tutorials on the subject; but what I would say is that there are very few things you can't do with a for loop, but there are several for which a while loop is not suited, so in general I would look at using a for-loop first. Experience will show you when the other forms are more relevant (or easier to read).

Winston

 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, anything you can do with a while loop can also be done with a for loop. And anything you can do with a for loop can also be done with a while loop. If your while loop didn't work, it's because there was something wrong with the way you'd coded it.

However, they are designed for different cases. Your code is going to be much easier to read if you use them in situations that match that design. And the general rule is:

- If you know in advance how many times you need to iterate, use a for loop
- If you don't know how many times you need to iterate, but you're just going to keep going until some condition is reached, use a while loop
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tto expand on what Matthew B has said already:
A while loop is designed for “sentinel‑controlled” iteration; you keep going until the sentinel value tells you to stop.
A for   loop is designed for “counter‑controlled” iteration; you keep going until you reach a particular count.
 
Winston Gutkowski
Bartender
Pie
Posts: 9484
50
Eclipse IDE Hibernate Ubuntu
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:A for   loop is designed for “counter‑controlled” iteration; you keep going until you reach a particular count.

I would just add that a for loop has one other advantage, which may make you pick it over a more obvious choice:
It allows you to define variables that are only used in the loop.

Obviously, you shouldn't abuse it; but it can be quite useful when you have a few fields that are clearly linked to each other.

Winston
 
Robel Bayisa
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These explanations really made things clearer for me. Thanks so much guys!
 
Campbell Ritchie
Sheriff
Pie
Posts: 47293
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You’re welcome and it is good when somebody understands something for the first time.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic