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 inability to control our childrens behavior feels the same as not being able to control it in ourselves. And the fact is that primitive behavior in children does unleash primitive behavior in mothers. Thats what frightens mothers most. For young children, even when out of control, do not have the power to destroy their mothers, but mothers who are out of control feel that they may destroy their children.”
—Elaine Heffner (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)