Recent

Author Topic: Pascal-style case and C-style break  (Read 1161 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 5595
Re: Pascal-style case and C-style break
« Reply #15 on: September 30, 2022, 11:13:40 am »
BTW, I don't miss that particularity of the C-switch, besides one very special case of https://forum.lazarus.freepascal.org/index.php/topic,60218.msg450448.html#msg450448

I don't miss it, since I'm only a C programmer rarely and under duress. However since dislike of the Pascal-style case statement has been a longstanding grumble among non-devotees, I found myself wondering what the opposite of (C-style) break would be since neither (Pascal-style) break nor continue can be used on account of their messing up a surrounding while or repeat, and at that point it occurred to me that certainly in my code this was about the only place that goto was being used.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BeniBela

  • Hero Member
  • *****
  • Posts: 847
    • homepage
Re: Pascal-style case and C-style break
« Reply #16 on: September 30, 2022, 02:35:35 pm »

The goto statement can already target numeric destinations, however these need to be declared as a label nevertheless and that will stay, because Pascal is a language where things need to be declared before use.

Perhaps he meant with numeric goto that you could address the label indirectly


Not this

Code: Pascal  [Select][+][-]
  1. procedure huh;
  2. label 0,1,2;
  3. begin
  4.   goto 1;
  5.   0: writeln('a');
  6.   1: writeln('b');
  7.   2: writeln('c');
  8. end;
  9.  

Not this

 

Code: Pascal  [Select][+][-]
  1. procedure huh;
  2. label 0,1,2;
  3. var
  4.   i: Integer;
  5. begin
  6.   i := 1;
  7.   case i of
  8.   0: writeln('a');
  9.   1: writeln('b');
  10.   2: writeln('c');
  11.   end;
  12. end;
  13.  

but this

Code: Pascal  [Select][+][-]
  1. procedure huh;
  2. label 0,1,2;
  3. var
  4.   i: Integer;
  5. begin
  6.   i := 1;
  7.   goto (i);
  8.   0: writeln('a');
  9.   1: writeln('b');
  10.   2: writeln('c');
  11. end;
  12.  

Thaddy

  • Hero Member
  • *****
  • Posts: 12209
Re: Pascal-style case and C-style break
« Reply #17 on: September 30, 2022, 02:46:44 pm »
Simply = without else....
Manuals, manuals, manuals first.
You have incompetence and sheer incompetence.

MarkMLl

  • Hero Member
  • *****
  • Posts: 5595
Re: Pascal-style case and C-style break
« Reply #18 on: September 30, 2022, 02:49:21 pm »
Perhaps he meant with numeric goto that you could address the label indirectly

Yes, as in the example I posted when I started the thread. Also Sven makes the point that

Quote
...the branches of a case-statement are not labels, because they can be ranges as well and thus even if non-declared labels could be used you still can't target the branches of a case-statement with that.

while I was musing on eliminating labels in their entirety and only using goto as the antithesis of (C-style) break in the context of a (Pascal-style) case statement.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Zvoni

  • Hero Member
  • *****
  • Posts: 1539
Re: Pascal-style case and C-style break
« Reply #19 on: September 30, 2022, 02:59:20 pm »
Or, as Zvoni wrote, in that particular case with an extra check:
Code: Pascal  [Select][+][-]
  1.      expectOptionalEtx, expectBcc:
  2.                              begin
  3.                                if (RecvState = expectOptionalEtx) then begin
  4. //           expectOptionalEtx: if ch = ETX then begin
  5.                                  bccCount := BccLength;
  6.                                  recvState := expectBcc;
  7.                                  if (ch = ETX) then begin
  8.                                    buffer += ch;
  9.                                    break;
  10.                                  end;
  11.                                end;
  12. //            expectBcc:     begin
  13.                              if not (ch in ['!'..'~']) then
  14.                                break;
  15.                              buffer += ch;
  16.                              bccCount -= 1;
  17.                              if bccCount = 0 then
  18.                                recvState := completed
  19.                            end
  20.           otherwise
  21. ...
My statement was more along the lines of
Code: Pascal  [Select][+][-]
  1. function TSerialThread.recvMessage(serHandle: TSerialHandle; var buffer: string;
  2.                                 timeout, limit: integer): boolean;
  3.  
  4. const
  5.   obeyTimeout= true;                    (* Normally true                        *)
  6.  
  7. type
  8.   tRecvState= (expectSOH, expectAddress, expectSeq, expectCtrl, expectPayload,
  9.                                 expectHandshake, expectOptionalEtx, expectBcc,
  10.                                                                     completed);
  11.         Function FuncExpectBCC(Const Ach:Char;var AbccCount:Integer;var ArecvState:tRecvState):Boolean;
  12.         Begin          
  13.                 Result:=False; 
  14.                 if not (Ach in ['!'..'~']) then
  15.                    Exit(True);
  16.                 buffer += ch;
  17.                 AbccCount -= 1;
  18.                 if AbccCount = 0 then
  19.                    ArecvState := completed;                      
  20.         End;
  21.  
  22. ........
  23.  
  24.         expectOptionalEtx: if ch = ETX then begin
  25.                              buffer += ch;
  26.                              bccCount := BccLength;
  27.                              recvState := expectBcc (* To process next character *)
  28.                            end else begin
  29.                              bccCount := BccLength;
  30.                              recvState := expectBcc;
  31.                              If FuncExpectBCC(ch,bccCount,recvState) Then Break;
  32.                            end;
  33.         expectBcc:     If FuncExpectBCC(ch,bccCount,recvState) Then Break
  34.       otherwise
  35.         break                           (* Quite simply can't happen            *)
  36.       end
  37.   until obeyTimeout and Elapsed(limitTD, limit);
  38.  
No Goto or labels or other such nonsense
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

y.ivanov

  • Hero Member
  • *****
  • Posts: 562
Re: Pascal-style case and C-style break
« Reply #20 on: September 30, 2022, 03:29:56 pm »
@Fallthrough:
Huh? Just use consecutive If-Then with an Else-Clause for the last if
If you need to exit the "switch" prematurely move the whole thing to a Function/Procedure
It is my misunderstanding, I thought you've meant to merge cases (the fall through case and the previous one) and then to use if-then to split them again in  the compound operator.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

PascalDragon

  • Hero Member
  • *****
  • Posts: 4765
  • Compiler Developer
Re: Pascal-style case and C-style break
« Reply #21 on: September 30, 2022, 03:30:54 pm »

The goto statement can already target numeric destinations, however these need to be declared as a label nevertheless and that will stay, because Pascal is a language where things need to be declared before use.

Perhaps he meant with numeric goto that you could address the label indirectly

I'm aware of what he meant. Which is why I added the part about the branches of the case-statement.

Zvoni

  • Hero Member
  • *****
  • Posts: 1539
Re: Pascal-style case and C-style break
« Reply #22 on: September 30, 2022, 04:54:12 pm »
@Fallthrough:
Huh? Just use consecutive If-Then with an Else-Clause for the last if
If you need to exit the "switch" prematurely move the whole thing to a Function/Procedure
It is my misunderstanding, I thought you've meant to merge cases (the fall through case and the previous one) and then to use if-then to split them again in  the compound operator.
Ahhh….. ok, now i understand.
Agreed. That is still my technique if i need fall-through.
But any „fall-through“ scenario has to be coded very carefully (order of conditions etc.)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018