Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do Pascal control structures appear to be inconsistent?

Most Pascal control structures make sense to me, like:

for ... do {statement};

if (condition) then {statement};

while (condition) do {statement};

where the {statement} is either a single statement, or a begin ... end block. I have a problem with:

repeat {statement-list} until (expression);

try {statement-list} except {statement-list} end;

Wouldn't it be better that repeat and try have the same general structure, accepting only a single statement or a begin ... end block, instead of having a statement-list that's not formally blocked with a begin and an end?

like image 342
70Mike Avatar asked Jan 06 '11 23:01

70Mike


2 Answers

The forms that require a begin/end all exist on a single line--the compiler has no other way to know where the block ends. The forms that don't require a begin/end have a closing structure that tells the compiler where it ends and thus the begin/end would simply be redundant. You're free to use it in this case if you want, though.

like image 107
Loren Pechtel Avatar answered Nov 09 '22 10:11

Loren Pechtel


Niklaus Wirth (the designer of Pascal) corrected these inconsistences in his next language, Modula-2. In Modula-2, compound statements like IF have no BEGIN and a mandatory END:

IF {condition} THEN
    {statement-list}
END;

Other control structures such as WHILE are similar and consistent with REPEAT.

like image 37
Greg Hewgill Avatar answered Nov 09 '22 10:11

Greg Hewgill