Tuesday, 14 August 2007

The Delphi/Pascal Sentence

...or "When/Where to use semicolons".

Apologies to all you long time Pascal gurus, but in the last few days I was pondering on the advice I gave a few students learning Pascal and felt that Today's post should be directed at those learning the language.

Many new-comers find it difficult to grasp when they should be using a semicolon. The rules seem complex and inconsistant to them, when in fact they are reasonably consistant. Consider this...

"Pascal is a sentence"

Writing in Delphi Pascal is writing sentances, its that simple. Consider the following simple piece of code...

if x > 0 then
ShowMessage('x is worth something')
else
ShowMessage('x is worthless');



That equates to a sentence of "If x is greater than zero then x is worth something otherwise x is worthless.". Some people call that a Pascal statement, but I like to think of it as a sentence. Sentences are normally ended in a full stop, but in Pascal, sentances are ended in a semicolon and only the program is ended in a full stop. in the above Pascal sentence, the semicolon comes at the end as it should, and not half way through. You wouldn't say "If x is greater than zero. Then x is worth something. Otherwise. X is worthless." would you? That's 4 parts of a sentence that are seperated into 4 sentences that are meaningless.

So why can't we just place a semicolon before the else? Well think of it. If someone is talking to you in fluent english and finishes the sentence, then a little later (when you are thinking of something else), states "otherwise...", you would say "Huh? what do you mean 'Otherwise'". The Delphi Pascal compiler would state the equivelent of "Huh?" at that point and totally fail to understand you.

"OK-ish", I hear you saying "but what about all that begin/end stuff, nobody says that in real life", and normally you'd be right. Consider the following...


if x > 0 then
begin
y := x;
ShowMessage('x and y are the same');
y := y * 10;
ShowMessage('and now they are not')
end;



How does that equate to a sentence? Well, there is an overriding sentence there that says something like "If x is greater than zero then do some things...". (There's more to that sentence that I'll add soon).

You can think of begin/end as brackets if you like, that's what C and some other languages do, but Pascal is ever so slightly different. When speaking fluent Pascal (Pascal is a language after all), and you want to say some other sentences in the middle of your main sentence, you will need to let the listener know that by saying 'begin' and 'end'.

Each of the lines ending in a semicolon are their own sentences: "make y equal to x"; "say 'x and y are the same'"; "make y equal to y times 10". Being sentences in their own right, they can even have their own begin/ends for their sentences if they need them.

What about that last ShowMessage() statement? Well, here's where the Pascal sentence comes in. The overriding sentence actually says "If x is greater than zero then do some things and say 'and now they are not'.". More correctly, in fluent Pascal the true sentence is "If x is greater than zero then begin do some things and say 'and now they are not' end."

When speaking Pascal you do have to say the 'begin' and 'end' words in a sentence.

Now I can also hear you saying "but I have seen a semicolon before the 'end' many times". Yes, and pascal is smart enough to understand that you are saying nothing. Placing a semicolon before the 'end' is the equivilent of stating an empty sentence before finishing the outer sentence. Sweet nothings.

So what about Procedures and Functions? Procedures and functions are just teaching the Pascal language new words to use. For example...


Procedure Swap(var x, y: integer);

...says "I'm going to teach you the word 'Swap' and it will involve 2 whole numbers". Here's the full procedure which takes 2 numbers and swaps their values so that x equals what y did and y equals what x did (normally used in something like a simple shell sort) ...


Procedure Swap(var x, y: integer);
var i : integer;
begin
i := x;
x := y;
y := i
end;



In this case, the sentence doesn't flow unless you expand it a little.

"Here's a new Pascal Word 'Swap' that takes 'x' and 'y' as variable parameters.
'i' is a new variable just for the word 'Swap'.
begin do some things and then make y equal i end.

The 'do some things' were some other sentences as you can see.

Are you now more confused than ever? Don't worry, that's normal, you are becoming a programmer after all :-)

7 comments:

  1. but it isn't english, comparisons are meaningless.

    ReplyDelete
  2. Last week I came across this C# online tutorial:

    http://www.vijaymukhi.com/

    If you scroll down on the getting started page, you'll get this nice explanation:

    --------------------

    The error says ';' expected. Though it is obvious to us that the statement has ended, unfortunately for you and me, C# isn't so smart. It requires a semi-colon to tell it loud and clear that the statement has ended. Merely pressing enter does not suffice. Though not so for other programming languages, in C# it is mandatory to use a semi-colon. Alas! Each programming language has its own rules!

    At this juncture, an intelligent question would be - But why semi-colon? Why not any other punctuation mark? We don't know for sure but perhaps the developers of C# were asked to select a character that would indicate the end of a statement. The non-compromising species that we are, they could not arrive at a consensus. Result? Grandma's recipe of eene meene mina mo! When they stopped, their finger was pointing at the semi-colon, hence they selected the same.

    Thus rules are rules; as preposterous as they may sound, they must be followed.

    ------------------------

    I guess they used Anders Hejlsberg's finger to do the trick...

    ReplyDelete
  3. An interesting discussion on the semicolon in C#. It makes perfect sense of course, considering where Anders came from :-)

    The idea that the statement ends with the end of line takes me back to Basic days (hated MSBasic). Pascal is much smoother and more easily read with statements across several lines as needed, ending with the semicolon.

    Why didn't the sentence end with a full stop? Well, that's because its still talking of course, right up till the end of the program ... where you will find the full stop. ;-)

    ReplyDelete
  4. Longtime Algol ProgrammerAugust 15, 2007

    So wrong it isn't funny. Semicolons separate statements, they don't terminate them. This isn't Cobol.

    C, Java, C# are different, they use random syntax (C: you need a semicolon except when you can't have one).

    ReplyDelete
  5. To Algol, I disagree.

    Either "Separate" or "terminate" a statement - its meaning is the same (unless you hold to some specific meaning presented by some book you once read about Algol or Cobol).

    If I said to you "That is a tree." and then stated "That is a bus.", both are separate statements (sentences) ended with a full stop. By comparing Pascal statements to Sentences, a new programmer can more easily understand where to place the semicolon.

    I'm afraid that I don't follow your point for the rest regarding other languages.

    Please can you write your comments in a less derogotory nature.

    ReplyDelete
  6. ....and if you still don't get it, then there is Visual Basic just for you.

    :P
    ...just joking my friends. I know that this knowledge is like math, a damn so natural thing that it could be ( someway :P ) hard to get.

    ReplyDelete
  7. Mark AndrewsMarch 25, 2008

    Steve Peacocke said "Either 'Separate' or 'terminate' a statement - its meaning is the same"

    In the context of the Pascal/Delphi language syntax these are different things.

    For example, consider the compound statement:

    begin

    ProcA ;
    ProcB

    end

    The semicolon is required after ProcA to separate it from ProcB. A semicolon is not required after ProcB. (In C it would be required, since semicolons terminate statements.) Pascal/Delphi allows a semicolon after ProcB as a courtesy, since most programmers aren't that familiar with the grammar.

    ReplyDelete

Note: only a member of this blog may post a comment.