Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Pascal was originally designed as a teaching language to encourage block-structured programming. Kernighan (the K of K&amp;R) wrote an (understandably biased) essay on Pascal's limitations, <a href="http://www.lysator.liu.se/c/bwk-on-pascal.html" rel="nofollow noreferrer"><em>Why Pascal is Not My Favorite Programming Language</em></a>.</p> <p>The prohibition on modifying what Pascal calls the <em>control variable</em> of a <code>for</code> loop, combined with the lack of a <code>break</code> statement means that it is possible to know how many times the loop body is executed without studying its contents.</p> <p>Without a <code>break</code> statement, and not being able to use the control variable after the loop terminates is more of a restriction than not being able to modify the control variable inside the loop as it prevents some string and array processing algorithms from being written in the "obvious" way.</p> <p>These and other difference between Pascal and C reflect the different philosophies with which they were first designed: Pascal to enforce a concept of "correct" design, C to permit more or less anything, no matter how dangerous.</p> <p>(Note: Delphi does have a <code>Break</code> statement however, as well as <code>Continue</code>, and <code>Exit</code> which is like <code>return</code> in C.)</p> <p>Clearly we never <strong>need</strong> to be able to modify the control variable in a <code>for</code> loop, because we can always rewrite using a <code>while</code> loop. An example in C where such behaviour is used can be found in K&amp;R section 7.3, where a simple version of <code>printf()</code> is introduced. The code that handles <code>'%'</code> sequences within a format string <code>fmt</code> is:</p> <pre><code>for (p = fmt; *p; p++) { if (*p != '%') { putchar(*p); continue; } switch (*++p) { case 'd': /* handle integers */ break; case 'f': /* handle floats */ break; case 's': /* handle strings */ break; default: putchar(*p); break; } } </code></pre> <p>Although this uses a pointer as the loop variable, it could equally have been written with an integer index into the string:</p> <pre><code>for (i = 0; i &lt; strlen(fmt); i++) { if (fmt[i] != '%') { putchar(fmt[i]); continue; } switch (fmt[++i]) { case 'd': /* handle integers */ break; case 'f': /* handle floats */ break; case 's': /* handle strings */ break; default: putchar(fmt[i]); break; } } </code></pre>
 

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