1
$\begingroup$

I'm abit afraid to ask this question here seeing as I asked it on programmers SE already, the thing is I think the question is more about the underlying theory than the use in practice (or call it confirmation bias). (If people here agree i should not have asked it here I will delete it and offer my sincere apologies) however I do think it will get a deeper meaning here as to why this is or isn't true.

Here is the question:
I have a teacher who (to me) seems quite intelligent, he really knows what he is talking about but he said something which surprised me (I'm paraphrasing): while loops can be proven where as for loops cannot.
I think this translates to: One can completely explain what happens in a while loop whereas you cannot do that for a for loop

Is my teacher right? (maybe this was the case when he started learning? ) and a more subjective question: Is there any clear reasoning behind this (why he would have stated this)?

$\endgroup$

closed as off-topic by David Richerby, Juho, Luke Mathieson, D.W., Gilles Apr 22 '15 at 12:51

  • This question does not appear to be about computer science within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ No, it seems wrong, since you can convert any for loop into a while loop. I could see the converse being true: if you have a for loop iterating over a range and you never touch the counter, then you have guaranteed Halting, which makes proving easier, but not automatic. $\endgroup$ – jmite Apr 21 '15 at 10:11
  • $\begingroup$ you are not guaranteed halting for(x=1,x<10,x++ ) {while(1)} $\endgroup$ – revisingcomplexity Apr 21 '15 at 10:11
  • 1
    $\begingroup$ I'm not sure why you're reposting this here. The answers on Programmers.SE (especially the one pointing out that for and while loops are completely equivalent) already seem to give a full answer to the question. What perspective are you looking for us to offer that hasn't already been covered over there? $\endgroup$ – David Richerby Apr 21 '15 at 10:16
  • $\begingroup$ @DavidRicherby I felt giving a code example for changing a for loop to a while (and vice versa) was not proof rather an example for which it could work. i thought perhaps there was a deeper underlying proof for why this is true. $\endgroup$ – Vincent Apr 21 '15 at 10:21
  • $\begingroup$ @VincentAdvocaat You are correct that giving just the construction for the translation is not enough of a proof. However, a proof is often just as much as needed to convince an expert that it works. Full proof formalization is useful, done more and more with computer tools, but it is very time and energy consuming. The main problem is not that it is wrong, but that it applies only to C. $\endgroup$ – babou Apr 21 '15 at 12:25
5
$\begingroup$

In a nutshell: What your teacher probably meant is that the semantics of while is pretty much the same in most languages, while the semantics of for may change considerably (see discussion below). Hence, abstract language independent proof are more reliable with a while, but one should be careful that a proof with a for loop may not match the semantics of the for loop in many languages.

Your question is not precise enough (though that may not be your fault).

The point is that, afaik, there is no official, ISO supported standard, or otherwise officially accepted reference definition of for and while loops. The definition depends on the programming language.

Hence you cannot make any general statement regarding their equivalence before you have defined precisely what each can do. I adress that more precisely, since it is one of the main argument used in other answers (and the discussion will be useful in what follows).

On intertranslatability of for and while loops

Summary: it depends on the programming language, but is always possible a long as you can have one infinite loop and a way to get out of it.

But you can make such a statement for a specific programming language, and the answer will depend on the features fo the language.

That also means that there is no general proof, but only one for each programming language.

One thing that is generally true is that a while loop can generally mimic a for loop, because the while loop can do the exit condition testing of the for loop, doing the initialisation of the control variable with an assignment before entering the loop, and doing the incrementation at the end of the loop body, so that

for i from 1 by 2 to 10 do { xxx }

becomes

i=1
while i≤11 do { xxx; i←i+2 }

This more or less works for most languages, but it is not as obvious as it seem, and there may be many "details" to worry about.

For example, in many languages, the for loop evaluates it 3 arguments (initial value, increment, and final value) as strict arguments, evaluated once before entering the loop, while others will take then as thunk arguments to be reevaluated at each turn, or possibly as lazy argument to be evaluated only when first needed.

Another point may be that the increment variable may be local to the for loop, or have to be a local variable of the function where the loop appears.

Depending on such issues, the translation of a for to a while may vary widely, though it is usually possible to achieve it.

The same holds for the converse, thranslating a while into a for loop.

Th first problem is that a while loop will always reevaluate the exit condition at each turn. But some for loops do not provide for a condition that is reevaluated at each turn, other than comparison of the control variable with some fixed value computed on loop entry. Then the translation is not possible unless there is some other mean to jump out of the loop on some arbitrary conditions.

That is achievable with various devices, usually starting with a conditional statement testing the condition, followed by an a jump out implemented, as available, by a loop exit statement, a return statement (after encapsulating the loop in a function), a goto statement or an exception raising.

In other words, it is again very dependent on languages, and possibly on subtle features of languages.

This say, as answered by @milleniumbug, the intertranslation is easy in the language C, because a for lopp is essentially a while loop plus some extra for an incremented control variable.

But this does not necessarily apply to other languages, and most likely not in the same way.

This being said, programming languages are usually supposed to have Turing power with only one of these loops, since all you need for it is one infinite loop. So, as long as you have some way of looping for ever, and possibly deciding to stop, you are pretty sure you can mimic any other construct ... but not necessarily easily.

Regarding proofs

Summary: There is no reason known to me to assert that proofs should be significantly harder with one or the other (unless some weird feature of the language).

There is probably a misunderstanding, or your teacher had his mind on something else.

Formals semantics can be defined for the various kinds of loops defined in programming languages, and then used for proving properties.

It may be, again depending on the language, that conducting formal proofs regarding programs may be more complex in some cases. But that depends on the language.

I cannot imagine a reason why proofs should be significantly harder with one construct more than with the other. The for loop may be more complex since it can offer, as in C, all that is done with a while plus other things. But if you did it with a while, you would have to add the extras in some other form.

I could use the formal general argument of intertranslatability, as long as there is the possibility for a single infinite loop. I will however refrain from doing that, as the constructions involved are nothing you want to deal with in a proof, and it would clearly be an unfair statement, at least in practice.

Following the above discussion, however, we have seen that the difficulties for intertranslatability come from the great variability of the for loop from language to language. Hence the following conclusion which is probably the right answer:

One possibility to understand your teacher's statement is that the semantics of the while loop is pretty much the same in all programming languages, while the syntax and semantics of the for loop can vary significantly from language to language. Hence, it is possible to make general "abstract" proofs with while loops that have language independent semantics to a good extent, while this is not possible for the for loop that has syntax and semantics changing too much from language to language. But this does not apply within a given language, when the semantics of both are precisely defined.

My best suggestion is that you should ask your teacher what he precisely meant, and whether he can give you an example. Misphrasing or misunderstanding is a common event.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.