Ross Levis asked:

> I have another beginners question.  The following generates the compiler
> error ' ; not allowed before ELSE'.

The other posts have address the practical solutions to this problem, but
there is a deeper issue here that should be mentioned for beginners to chew
on.

Nicholas Worth designed the base Pascal syntax to use the semi-colon
character as a statement sepeator, and this has confused Pascal students for
years now. It is especially annoying to anyone who is used to the C syntax
where a semi-colon is used as a statement terminator.

The difference manifects itself most obviously in the if/then/else parts of
the language. The syntax for this reads something like (hope you can read
BNF!):

  if expression then statement1 [else statement2]

Notice that there is no ; between the statement1 and the else. Semi colons
are only used in the following syntax areas:

  statement ::= simple-statement | block-statement

  block-statement ::= begin statement [; statement] end

where the ; is seperating two statements that are at the same level in the
code. So the semi colon as designed is only used to seperate two adjacent
statements so that the parser can distinguish where one ends and the next
starts. And you'll also notice that you don't need a semi-colon nefore an
end, because the parser knows that this is the end of the block; If you do
have a semicolon before the end you are actually introducing a new statement
that is empty, which luckly is a valid statement type in Pascal, or things
would be even more confusing than now.

One of the fun challenges that you can have a go at when writing Delphi code
is to see how few semi-colons you can get away with. As you never need semi
colons before else, end, finally, except and a few other places you can use
suprisingly few, although otherson your team may start reviling your code
8-)

And because C has it that every statement must terminate with a semi-colon,
which is actually defined in the language specification to mean throw any
any results of the preceeding statement, you find that you have to have
semi-colons before elses in exactly the same way that Pascal doesn't allow.
And when you switch back and forth you brain slowly starts to fail under the
pressure of remembering these different rules.

Cheers, Max.


---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz
To UnSub, send email to: [EMAIL PROTECTED] 
with body of "unsubscribe delphi"

Reply via email to