3

This is a core question please don't say with regard to syntax or semantics, the question is that what is the actual difference between

WHILE loop and FOR loop, everything written in for loop can be done with while loop then why two loops?

This is asked in a seminar at the university of Cambridge. so i think we have to explain in terms of performance overheads and WC complexity.

I think we have to go in terms of Floyd-Hoare logic

aioobe
  • 413,195
  • 112
  • 811
  • 826
cc4re
  • 4,821
  • 3
  • 20
  • 27
  • 1
    Trying to preserve the spirit of the original question, would a better way of putting this question be: "Given two semantically equivalent loop blocks: one written using a for loop, one written using a while. What differences can we expect in the generated bytecode (in exceptional cases and otherwise)?" (Observing the Java, performance and compiler tags) – Felix Fung Sep 25 '12 at 02:21
  • ok let me explain it in another moment in this thread. – cc4re Sep 25 '12 at 02:31
  • I wouldn't say that "there is no performance difference between..." because this question regards the _semantics_ of Hoare logic, performance doesn't come into the picture at all, this is merely an abstract semantics, a real machine implementation will respect the semantics at a high level, but will be given by way of a much more complex semantics (which allows the possibility to encode complexity..). I.e., you shouldn't conflate this kind of semantics with performance _at all_, they have nothing to do with each other, really.. – Kristopher Micinski Sep 25 '12 at 03:07

6 Answers6

1

As far as performance overheads go, that will depend on the compiler and language you're using.

The major difference between them is that for loops are easier to read when iterating over a collection of something, and while loops are easier to read when a specific condition or boolean flag is being evaluated.

Jonathon Ashworth
  • 1,182
  • 10
  • 20
  • 1
    He is posting in the Java tag so I am guessing his language is Java. AFAIK, there is absolutely no difference in performance between for and while loops in Java. But correct on the second point. – mbinette Sep 25 '12 at 02:21
  • Ah, in that case another thing to mention is that Java provides foreach functionality with it's for loops. – Jonathon Ashworth Sep 25 '12 at 02:24
  • I also choose which construct to use based on these criteria. Using a `for` to loop until a condition is met sounds unnatural to me. The same way I feel over using a `while` to iterate over a collection. – André Oriani Sep 25 '12 at 02:24
1

The only difference between the two looping constructs is the ability to do pre-loop initialization and post-loop changes in the header of the for loop. There is no performance difference between

while (condition) {
    ...
}

and

for ( ; condition ; ) {
    ...
}

constructs. The alternative is added for convenience and readability, there are no other implications.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The difference is that a for() loop condenses a number of steps into a single line. Ultimately, all answers will boil down to syntax or semantics in this way, so there can't be an answer that really pleases you.

A while() loop only states the condition under which the loop will terminate, while the for() loop can stipulate a variable to measure by as well as a counter in addition to this.

(Actually, for() loops are very versatile and a lot can happen in their declarations to make them pretty fancy, but the above statement sums up almost all of the for() loops you will ever see, even in a production environment.)

asteri
  • 11,402
  • 13
  • 60
  • 84
  • if you can attain what for loop does with while loop then what is the need for both ? – cc4re Sep 25 '12 at 02:18
  • There isn't a "need" for both. You don't need `for()` loops, technically speaking. Because, as you said in your post, anything -- literally, *anything* -- that can be done with a `for()` loop can be done with a `while()` loop instead. It just requires more complexity within the loop to make it run in a `while()`, usually. – asteri Sep 25 '12 at 02:20
0

One occasionally relevant difference between the two is that a while loop can have a more complex condition imposed on it than a for loop

The FOR loop is best exployed when looping through a collection of predictable size

This I guess would be considered syntactical, but I consider it to refute the concept that the for loop can do anything the while loop can, though the reverse is indeed correct

celem
  • 404
  • 3
  • 10
  • Are you sure you can't use || and && operators inside the evaluation of a for loop in Java? – Jonathon Ashworth Sep 25 '12 at 02:26
  • I was, now I have to doublecheck to ensure i didnt put my foot in my mouth...be right back – celem Sep 25 '12 at 02:27
  • I was under the impression that those operators could be used in any boolean expression. I can't see why the one in a for loop would be any different, though I may be wrong, I can't remember ever doing this! – Jonathon Ashworth Sep 25 '12 at 02:28
  • http://wiki.answers.com/Q/Difference_between_a_for_loop_and_while_loop This is what I was thinking of when I stated the above. a for loop has a very static initialisation – celem Sep 25 '12 at 02:31
  • J. Ashworth is right. celem: "a && b" is considered an `expression`. – mbinette Sep 25 '12 at 02:33
  • indeed, i've edited to reflect this, good point. However as the link in my above comment points out, the while loop can still handle a more complex set of conditions than the for – celem Sep 25 '12 at 02:40
0

There is obviously no need for two loops, you need only one. And indeed, many textbooks consider a language (usually named Imp) which desugars for to while with an initialization statement before the while.

That being said, if you try working out the difference in the loop invariants, and associated rules in Hoare logic for these two loops, they differ just a bit, because of the init block. Since this looks like homework, I'll let you work out the details.

So the answer is: "It makes things just a little easier, on the syntax and proof side, but it's merely cosmetic, for all intents an purposes you really only need one..."

So far none of the other answers point out what I think the really important part of the distinction is for your question: which is probably that they change things a little bit with the Hoare connectives...

Also, note that speculating on performance based on Hoare logic is silly. If you care about the semantics, your professor probably doesn't give a damn or want to hear about performance :-)

Kristopher Micinski
  • 7,572
  • 3
  • 29
  • 34
0

everything can be done with if-goto statement. so why any loops at all? it's a syntax sugar to make developer's life easier and to allow writing better structured (smaller and more readable) code. it has nothing to do with performance

piotrek
  • 13,982
  • 13
  • 79
  • 165