### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Syntax curiosity  (Read 4169 times)

#### winni

• Hero Member
• Posts: 2116
##### Re: Syntax curiosity
« Reply #15 on: September 12, 2019, 10:57:59 pm »
The original Wirth syntax said:

After every then or else is exact one statement allowed. If there are more then one statement you have to enclose them with begin ... end;

So this is allowed:

Code: Pascal  [Select][+][-]
1. If c = clBlue then doSomeThing else
2. if c = clLime then doAnotherThing else
3. if c = clRed then PaintItRed else
4.   begin
5.   MixedColors;
6.   canvas.TextOut (10,10,'Mixed Colors');
7.   end;
8.
9.

Winni

#### ArtLogi

• Full Member
• Posts: 144
##### Re: Syntax curiosity
« Reply #16 on: September 12, 2019, 11:45:47 pm »
The original Wirth syntax said:

After every then or else is exact one statement allowed. If there are more then one statement you have to enclose them with begin ... end;

So this is allowed:

Code: Pascal  [Select][+][-]
1. If c = clBlue then doSomeThing else
2. if c = clLime then doAnotherThing else
3. if c = clRed then PaintItRed else
4.   begin
5.   MixedColors;
6.   canvas.TextOut (10,10,'Mixed Colors');
7.   end;
8.
9.

Winni
Nice

Just for curiosity here is example of Forth (Stack based postfix language), replace DUP with any number (DUP is stack command = Duplicate last returned value/object). This makes much more sense than infix IFs:
Code: Text  [Select][+][-]
1. : EGGSIZE
2.    DUP 18 < IF  ." reject "      ELSE
3.    DUP 21 < IF  ." small "       ELSE
4.    DUP 24 < IF  ." medium "      ELSE
5.    DUP 27 < IF  ." large "       ELSE
6.    DUP 30 < IF  ." extra large " ELSE
7.       ." error "
8.    THEN THEN THEN THEN THEN DROP ;
From https://www.forth.com/starting-forth/4-conditional-if-then-statements/
« Last Edit: September 12, 2019, 11:49:55 pm by ArtLogi »

#### winni

• Hero Member
• Posts: 2116
##### Re: Syntax curiosity
« Reply #17 on: September 13, 2019, 12:57:40 am »
Gosh! Forth - I haven't used it for 30 years. When the PCs were awful slow, then Forth was fast as lightning. But I hate postfix notations - the same hassle as with PostScript.

And for the quintuple of "THEN" the should invent something shorter. Perhaps "THEN * 5".

#### 440bx

• Hero Member
• Posts: 2091
##### Re: Syntax curiosity
« Reply #18 on: September 13, 2019, 01:43:17 am »
This discussion about the chained if/else reminded me of how nicely that construct is implemented in COBOL85 and above.

In COBOL a programmer can simply have
Code: ASM  [Select][+][-]
1. EVALUATE TRUE
2.   WHEN <conditional expression 1>
3.     <statements applicable to above condition>
4.
5.   WHEN <conditional expression 2>
6.     <statements applicable to above condition>
7.
8.   WHEN <conditional expression n>
9.     <statements applicable to above condition>
10.
11.   WHEN OTHER
12.     <statements applicable to above condition>
13. END-EVALUATE
14.
fully linear and prioritized by the test sequence.  In addition to that, it allows multiple conditions to be tested making the implementation of logical tables simple and really easy to understand and maintain.  It's also possible to evaluate against FALSE, which can occasionally be very convenient.

That statement can easily be synthesized in Pascal using a nested procedure (or nested function if convenient) but, it is extremely rare to see it done that way.  Instead, long series of "else if" are all too common.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

#### jamie

• Hero Member
• Posts: 4036
##### Re: Syntax curiosity
« Reply #19 on: September 13, 2019, 02:26:57 am »
Forth wasn't so bad, I participated in a camera view project to detect defected package via a camera and operate a push rode to eject it from the packing line.

The idea was to test for  a defective label where as the label and bottle was almost the same color using Black and White camera.

Did that on a Z80 processing box using forth and some Zilog Asm..
The only true wisdom is knowing you know nothing

#### winni

• Hero Member
• Posts: 2116
##### Re: Syntax curiosity
« Reply #20 on: September 13, 2019, 02:45:32 am »
No - Forth wasn't bad. I liked the concept. And the speed. But I struggled with the postfix notation.

Z80 (Unused! Brandnew!) you can buy for 1.30 \$. At Ebay. "Play it again, Sam ......"

#### munair

• Hero Member
• Posts: 697
• keep it simple
##### Re: Syntax curiosity
« Reply #21 on: September 13, 2019, 07:01:20 am »
This discussion about the chained if/else reminded me of how nicely that construct is implemented in COBOL85 and above.

In COBOL a programmer can simply have
Code: ASM  [Select][+][-]
1. EVALUATE TRUE
2.   WHEN <conditional expression 1>
3.     <statements applicable to above condition>
4.
5.   WHEN <conditional expression 2>
6.     <statements applicable to above condition>
7.
8.   WHEN <conditional expression n>
9.     <statements applicable to above condition>
10.
11.   WHEN OTHER
12.     <statements applicable to above condition>
13. END-EVALUATE
14.
fully linear and prioritized by the test sequence.  In addition to that, it allows multiple conditions to be tested making the implementation of logical tables simple and really easy to understand and maintain.  It's also possible to evaluate against FALSE, which can occasionally be very convenient.

That statement can easily be synthesized in Pascal using a nested procedure (or nested function if convenient) but, it is extremely rare to see it done that way.  Instead, long series of "else if" are all too common.

I wouldn't recommend synthesizing such construct using procedures because it would be much less optimal with the added stack frames. Elegance is nice but not at the cost of less optimal code.

#### 440bx

• Hero Member
• Posts: 2091
##### Re: Syntax curiosity
« Reply #22 on: September 13, 2019, 07:49:31 am »
I wouldn't recommend synthesizing such construct using procedures because it would be much less optimal with the added stack frames. Elegance is nice but not at the cost of less optimal code.
You're right in that there is an added stack frame in that case but, the loss of performance is negligible and, only perceptible if the procedure is executed extremely often.

I consider code simplicity and maintainability much more important than performance.  I only pay attention to performance when it gets in the way of usability, i.e, jerky or "elastic" user interface and that kind of thing.   (The Windows API DrawText, used by Listview controls and wsprintf in user32 are in that category, their performance is dismal.)

ETA:

It's quite likely that such a procedure could be inlined thus eliminating the stack frame and performance considerations.  I haven't tried it but, it seems quite reasonable.

« Last Edit: September 13, 2019, 07:51:41 am by 440bx »
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

#### PascalDragon

• Hero Member
• Posts: 2592
• Compiler Developer
##### Re: Syntax curiosity
« Reply #23 on: September 13, 2019, 09:50:56 am »
Ever since I started programming with Pascal I wondered about the IF construct, specifically the two-keywords ELSE IF construct for elseif blocks. There are languages that use one keyword for it (VisualBASIC, Python, PHP), usually something like ELSEIF or ELSIF.
It's rather simple: Pascal does not have an ELSE IF construct. It simply has another if ... then (... else) statement inside the else-branch of an if-statement (or the then-branch). This reduces complexity in the parser, cause then it's just a chain of if-statements.

• Hero Member
• Posts: 10684
##### Re: Syntax curiosity
« Reply #24 on: September 13, 2019, 10:01:12 am »
Pascal has case....

#### munair

• Hero Member
• Posts: 697
• keep it simple
##### Re: Syntax curiosity
« Reply #25 on: September 13, 2019, 12:17:15 pm »
Ever since I started programming with Pascal I wondered about the IF construct, specifically the two-keywords ELSE IF construct for elseif blocks. There are languages that use one keyword for it (VisualBASIC, Python, PHP), usually something like ELSEIF or ELSIF.
It's rather simple: Pascal does not have an ELSE IF construct. It simply has another if ... then (... else) statement inside the else-branch of an if-statement (or the then-branch). This reduces complexity in the parser, cause then it's just a chain of if-statements.

Yes, code ordered nicely in this manner:

Code: Pascal  [Select][+][-]
1. if a then
2.   someA()
3. else if b then
4.   someB()
5. else if c then
6.   someC();

would logically be:

Code: Pascal  [Select][+][-]
1. if a then
2.   someA()
3. else
4.   if b then
5.     someB()
6.   else
7.     if c then
8.       someC();

People new to Pascal may confuse the first coding habit with C-like syntax where the ELSE IF combination (outside braces) actually is treated as a single statement.
« Last Edit: September 13, 2019, 12:50:55 pm by Munair »

#### 440bx

• Hero Member
• Posts: 2091
##### Re: Syntax curiosity
« Reply #26 on: September 13, 2019, 01:14:46 pm »
Pascal has case....
but a case statement cannot be used to replace an if/else chain.

FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

#### howardpc

• Hero Member
• Posts: 3608
##### Re: Syntax curiosity
« Reply #27 on: September 13, 2019, 01:43:46 pm »
Pascal has case....
but a case statement cannot be used to replace an if/else chain.
Nested case statements can replace an if/else chain, and are usually clearer, if more verbose.

#### 440bx

• Hero Member
• Posts: 2091
##### Re: Syntax curiosity
« Reply #28 on: September 13, 2019, 02:03:30 pm »
Nested case statements can replace an if/else chain, and are usually clearer, if more verbose.
I don't see how it could be done when the if statements compare an expression to another expression.  A case statement requires an expression to be compared with constant values.  I don't see how that limitation can be circumvented even with nested case statements.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

#### howardpc

• Hero Member
• Posts: 3608
##### Re: Syntax curiosity
« Reply #29 on: September 13, 2019, 02:31:29 pm »
I have in mind constructs of this sort
Code: Pascal  [Select][+][-]
1. program Project1;
2.
3. {\$mode objfpc}{\$H+}
4.
5. type
6.   TCategory = (Less, Equal, Greater);
7.
8.   function Categorise(aValue, aBoundary: Integer): TCategory;
9.   begin
10.     if aValue < aBoundary then
11.       Result := Less
12.     else if aValue = aBoundary then
13.       Result := Equal
14.     else if aValue > aBoundary then
15.       Result := Greater;
16.   end;
17.
18.   function CategoriseCase(aValue, aBoundary: Integer): TCategory;
19.   begin
20.     case aValue < aBoundary of
21.       True: Exit(Less);
22.       False: case aValue > aBoundary of
23.         True: Exit(Greater);
24.         False: Exit(Less);
25.       end;
26.     end;
27.   end;
28.
29. var
30.   value: Integer;
31.
32. begin
33.   WriteLn('Categorise(10, 99) = ',Categorise(10, 99));
34.   WriteLn('Categorise(99, 99) = ',Categorise(99, 99));
35.   WriteLn('Categorise(100, 99) = ',Categorise(100, 99));
36.   WriteLn;
37.   WriteLn('CategoriseCase(10, 99) = ',Categorise(10, 99));
38.   WriteLn('CategoriseCase(99, 99) = ',Categorise(99, 99));
39.   WriteLn('CategoriseCase(100, 99) = ',Categorise(100, 99));