Comparison of Pascal and C - Control Structures

Control Structures

Statements for building control structures are roughly analogous and relatively similar (at least the first three).

Pascal has:

  • if cond then stmt else stmt
  • while cond do stmt
  • repeat stmt until cond
  • for id := expr to expr do stmt and for id := expr downto expr do stmt
  • case expr of expr : stmt; ... expr : stmt; else: stmt; end

C has:

  • if (cond) stmt else stmt
  • while (cond) stmt
  • do stmt while (cond)
  • for (expr; cond; expr) stmt
  • switch (expr) { case expr : stmt; ... case expr : stmt; default: stmt }

Pascal, in its original form, did not have an equivalent to default, but an equivalent else clause is a common extension. Pascal programmers otherwise had to guard case-statements with an expression such as: if expr not in then default-case.

C has the so called early-out statements break and continue, and some Pascals have them as well. There is controversy about whether the inclusion of these statements is in keeping with structured programming methodology. The best that can be said about this is that the use of break and continue may make programming easier, but there is no case where they cannot be replaced by "orthodox" structured programming constructs.

Both C and Pascal have a goto statement. However, since Pascal has nested procedures/functions, jumps can be done from an inner procedure or function to the containing one; this was commonly used to implement error recovery. C has this ability via the ANSI C setjmp and longjmp. This is equivalent, but arguably less safe, since it stores program specific information like jump addresses and stack frames in a programmer accessible structure.

Read more about this topic:  Comparison Of Pascal And C

Famous quotes containing the words control and/or structures:

    The basic tool for the manipulation of reality is the manipulation of words. If you can control the meaning of words, you can control the people who must use the words.
    Philip K. Dick (1928–1982)

    The philosopher believes that the value of his philosophy lies in its totality, in its structure: posterity discovers it in the stones with which he built and with which other structures are subsequently built that are frequently better—and so, in the fact that that structure can be demolished and yet still possess value as material.
    Friedrich Nietzsche (1844–1900)