Recent

Author Topic: GoTo Statement  (Read 2464 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1042
GoTo Statement
« on: September 14, 2021, 06:35:48 am »
I have a procedure I can't make work. So I'm thinking of trying it with a GOTO statements.

If I set  {$GOTO ON}  and  {$GOTO OFF} as such  will that turn it off and within the block.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.SetSize( ASTRING : String);
  2.  var Line : String;
  3. begin
  4.   {$GOTO ON}
  5.     ....
  6.    GOTO  Label1:
  7.    ....
  8.   Label1:
  9.    ....
  10.    GOTO Label6:
  11.    ....
  12.   Label6:
  13.    ....
  14.  {$GOTO OFF}
  15. end;
  16.  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Zvoni

  • Hero Member
  • *****
  • Posts: 708
Re: GoTo Statement
« Reply #1 on: September 14, 2021, 08:25:00 am »
Huh? What's wrong with nested Function/Procedure?
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

JLWest

  • Hero Member
  • *****
  • Posts: 1042
Re: GoTo Statement
« Reply #2 on: September 14, 2021, 08:51:14 am »
I posted in beginners. I have never used a nested function/procedure but maybe.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

devEric69

  • Hero Member
  • *****
  • Posts: 572
Re: GoTo Statement
« Reply #3 on: September 14, 2021, 08:56:55 am »
Labels must be declared below variables: please, read https://wiki.lazarus.freepascal.org/Goto/fr and  https://wiki.lazarus.freepascal.org/Goto .
I really like to use labels inside loop ( and I'm not a newbie in turbo-Pascal ;) ):


Code: Pascal  [Select][+][-]
  1. for iFoo:= iThis to iThere the begin
  2.   ...\...
  3.   j:= calcHere;
  4.   if j = 0 then goto lblFooNext;
  5.   ...\...
  6. lblFooNext:
  7. end;
« Last Edit: September 14, 2021, 09:01:35 am by devEric69 »
use: Linux 64 bits (Ubuntu 20.04 LTS).
Lazarus version: 2.0.4 (svn revision: 62502M) compiled with fpc 3.0.4 - fpDebug \ Dwarf3.

Zvoni

  • Hero Member
  • *****
  • Posts: 708
Re: GoTo Statement
« Reply #4 on: September 14, 2021, 09:03:29 am »
I posted in beginners. I have never used a nested function/procedure but maybe.
Post your Procedure you can't get to work, and comment where you want when to jump to
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

PascalDragon

  • Hero Member
  • *****
  • Posts: 3335
  • Compiler Developer
Re: GoTo Statement
« Reply #5 on: September 14, 2021, 09:05:49 am »
I posted in beginners. I have never used a nested function/procedure but maybe.

Nested functions/procedures are simple:

Code: Pascal  [Select][+][-]
  1. procedure Outer;
  2. var
  3.   a: LongInt;
  4.  
  5.   procedure Inner1;
  6.   begin
  7.     b := 42;
  8.   end;
  9.  
  10. var
  11.   b: LongInt;
  12.  
  13.   function Inner2: LongInt;
  14.   begin
  15.     Result := b + a;
  16.   end;
  17.  
  18. var
  19.   c: LongInt;
  20. begin
  21.   Inner1;
  22.   b := 21;
  23.   c := Inner2;
  24. end;

Nested functions/procedures are able to access variables of the surrounding routine that were declared above them. So here Inner1 has access to a and Inner2 has access to a and b (and the surrounding routine has access to a, b and c).

Labels must be declared below variables: please, read https://wiki.lazarus.freepascal.org/Goto/fr and  https://wiki.lazarus.freepascal.org/Goto .

They don't need to be declared below variables, but they do need to be declared.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9536
  • FPC developer.
Re: GoTo Statement
« Reply #6 on: September 14, 2021, 09:29:42 am »
You don't give an exact errormessage. Also you don't declare labels with the LABEL statement, as the wiki does.


JLWest

  • Hero Member
  • *****
  • Posts: 1042
Re: GoTo Statement
« Reply #7 on: September 14, 2021, 05:37:07 pm »
Thanks to all.

I have a procedure with goto's working. My example was poor.

I'm trying to parse some text, which would fit into a 70 character window. If I can't get it working I'll post a demo of it.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Blade

  • Full Member
  • ***
  • Posts: 136
Re: GoTo Statement
« Reply #8 on: September 14, 2021, 06:31:51 pm »
I really like to use labels inside loop ( and I'm not a newbie in turbo-Pascal ;) ):

I use them too, from time to time, like for certain conditions to exit a long function.  Don't see anything wrong with it, as long as the person doesn't get excessive, and makes sure that the usage is clear and the code is easy to follow.

When a person is a beginner, it might be good advice to know that goto is one way among many ways, not the main way or always the "goto" for getting out of any coding problems.

Zath

  • Sr. Member
  • ****
  • Posts: 370
Re: GoTo Statement
« Reply #9 on: September 14, 2021, 11:48:22 pm »
From the earliest days of Delphi, we are told the Goto is not to be used, it is dangerous because you can jump out of anywhere to anywhere leaving all sorts of things open behind you.

It was fine in Basic when things were often sequential but not in today's Delphi/Pascal when there are so many other ways to achieve the same result.


Zvoni

  • Hero Member
  • *****
  • Posts: 708
Re: GoTo Statement
« Reply #10 on: September 15, 2021, 08:28:54 am »
From the earliest days of Delphi, we are told the Goto is not to be used, it is dangerous because you can jump out of anywhere to anywhere leaving all sorts of things open behind you.

It was fine in Basic when things were often sequential but not in today's Delphi/Pascal when there are so many other ways to achieve the same result.
What i said: What's wrong with nested Functions?
I don't think i have used a Goto in some 15 years of VB6/VBA, and i haven't used it once in 3 years of FPC/Lazarus....
Goto just invites Spaghetti (though i do like Spaghetti with Bolognese.....)
One System to rule them all, One IDE to find them,
One Code to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
People call me crazy, because i'm jumping out of perfectly fine aircraft

MarkMLl

  • Hero Member
  • *****
  • Posts: 3077
Re: GoTo Statement
« Reply #11 on: September 15, 2021, 10:56:36 am »
From the earliest days of Delphi, we are told the Goto is not to be used, it is dangerous because you can jump out of anywhere to anywhere leaving all sorts of things open behind you.

It was fine in Basic when things were often sequential but not in today's Delphi/Pascal when there are so many other ways to achieve the same result.

Short response: there are places where GOTO is valuable, but the OP's problem is not one of them: he needs to find a book on elementary Pascal programming.

Longer response: Dijkstra's criticism of GOTO was actually sensationalised by Wirth, and Dijkstra himself later remarked on the religious fervour with which some people try to avoid the construct. More recent languages tend to replace GOTO with one or more of break/exit/continue/next, but I've seen those derided as being no better.

I fully accept that unstructured control transfers and premature function termination jeopardise attempts at formal analysis and proof (term used in the strict mathematical sense) of correctness, but in practical terms we're all trying to get stuff done here and the type checking pioneered by Pascal (and gradually adopted by most successor languages) provides far more of a boost to productivity than do most "pure science" languages which are so incomplete as to be barely usable for Real World problems.

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

440bx

  • Hero Member
  • *****
  • Posts: 2469
Re: GoTo Statement
« Reply #12 on: September 15, 2021, 11:28:02 am »
The presence of a GOTO means one or more of the following things:

1. the programmer still has things to learn about programming (or is too lazy to spend the time to figure out a decent algorithm.)

2. the algorithm is poorly designed or poorly implemented or, both.
3. the language lacks basic control flow structures (e.g, SNOBOL)

The irony is, who needs a GOTO these days when exceptions, i.e, GOTO on steroids, are not only available but, objects of pride for some programmers and, are as common today as GOTOs were in "the good GOTO days".

Sometimes Computer Science makes great progress, unfortunately, sometimes in the wrong direction.
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 9536
  • FPC developer.
Re: GoTo Statement
« Reply #13 on: September 15, 2021, 11:42:22 am »
My Delphi code contains two gotos. They are in speed sensitive code jumping out of a double nested IF (and yes, that is faster, or at least till D2009, when I lsst tested).

The code strings runs together into BLOBs.

ccrause

  • Sr. Member
  • ****
  • Posts: 435
Re: GoTo Statement
« Reply #14 on: September 15, 2021, 11:48:10 am »
Goto just invites Spaghetti
IMHO the hype against GOTO should not be about avoiding GOTO, rather on putting effort into streamlining of logical flow (rather eat spaghetti than leave it in code form).  I've seen various innovative ways of programming spaghetti while cleverly avoiding the actual use of GOTO.  One method is to use an endless loop with various combinations of break, continue and exit statements that really mess with my understanding of the logical flow.  If this becomes necessary to express the logic, it may be time to consider other techniques, for example a state machine.

Apologies to JLWest for following this tangent.

 

TinyPortal © 2005-2018