Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In programming language theory (and in computability theory) <strong>WHILE and FOR loops have different theoretical properties</strong>:</p> <ul> <li><strong>a WHILE loop may never terminate</strong> (the expression could just be TRUE)</li> <li>the finite number of times a FOR loop is to execute is supposed to be known before it starts executing. <strong>You're supposed to know that FOR loops always terminate.</strong></li> </ul> <p>The FOR loop present in C doesn't technically count as a FOR loop because you don't necessarily know how many times the loop will iterate before executing it. (i.e. you can hack the loop counter to run forever)</p> <p>The class of problems you can solve with WHILE loops is strictly more powerful than those you could have solved with the strict FOR loop found in Pascal. </p> <p><strong>Pascal is designed this way so that students have two different loop constructs with different computational properties</strong>. (If you implemented FOR the C-way, the FOR loop would just be an alternative syntax for while...)</p> <p><strong>In strictly theoretical terms, you shouldn't ever need to modify the counter within a for loop.</strong> If you could get away with it, you'd just have an alternative syntax for a WHILE loop.</p> <p>You can find out more about "while loop computability" and "for loop computability" in these CS lecture notes: <a href="http://www-compsci.swan.ac.uk/~csjvt/JVTTeaching/TPL.html" rel="noreferrer">http://www-compsci.swan.ac.uk/~csjvt/JVTTeaching/TPL.html</a></p> <p>Another such property btw is that the loopvariable is undefined after the for loop. This also makes optimization easier</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload