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:

    Above and beyond paying attention to feelings before and after a separation, never threaten your child with leaving or loss of love in an effort to control her behavior. Children believe their parents’ assertions that “I will send you away,” “I won’t love you any more,” “I’ll go away,” and are terrified with good reason. Fear is a very poor way of disciplining a child, and it can cause severe lifelong anxiety.
    Alicia F. Lieberman (20th century)

    If there are people who feel that God wants them to change the structures of society, that is something between them and their God. We must serve him in whatever way we are called. I am called to help the individual; to love each poor person. Not to deal with institutions. I am in no position to judge.
    Mother Teresa (b. 1910)