Pascal

Moderators: None (Apply to moderate this forum)
Number of threads: 4095
Number of posts: 14004

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
Are there labels in Pascal? Posted by jim_giboney on 24 Feb 2003 at 3:41 PM
I know you can use Labels in Basic, and I think C++, but can you use them in pascal?

In basic, you would use:
LabelX:
//code
if a = 5 then
GOTO LabelX;
end


If there are labels (Surely there are, right?) how would you use them?
Report
Re: Are there labels in Pascal? Posted by Opius on 24 Feb 2003 at 4:15 PM
This message was edited by Opius at 2003-2-24 16:15:50

: I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
:
: In basic, you would use:
:
: LabelX:
: //code
: if a = 5 then
: GOTO LabelX;
: end
: 

:
: If there are labels (Surely there are, right?) how would you use them?
:


Labels/Goto statements are an integral part of spaghetti code. Fortunately (or unfortunately), there are no label/gotos in any strutctured programming language such as C++ or Pascal, at least as far as I know. An easy way to do this sort of thing is to use procedures and functions. Example

procedure RandomProcedureName(parameters);
   begin
      //code
   end;

begin
   //code
   if a=5 then
      RandomProcedureName(parameters);
end.


Functions operate in a similar way but must be used in an equation or output statement.

-Opius


Report
Re: Are there labels in Pascal? Posted by Manning on 24 Feb 2003 at 5:32 PM
: This message was edited by Opius at 2003-2-24 16:15:50

: : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
: :
: : In basic, you would use:
: :
: : LabelX:
: : //code
: : if a = 5 then
: : GOTO LabelX;
: : end
: : 

: :
: : If there are labels (Surely there are, right?) how would you use them?
: :
:
:
: Labels/Goto statements are an integral part of spaghetti code. Fortunately (or unfortunately), there are no label/gotos in any strutctured programming language such as C++ or Pascal, at least as far as I know. An easy way to do this sort of thing is to use procedures and functions. Example

Personally I havent ever used GOTO, but like everything there is a time and place for it. And Pascal does support it. Here is an example (written from memory, it may not be syntactically correct)

procedure Test;
label
  Bottom;
var
  Ch: Char;
begin
     while (1=1) do
     begin
          Ch := ReadKey;
          if (Ch = #27) then
             Goto Bottom;
     end;

:Bottom
     WriteLn('Used GOTO to exit the loop');     
end;

Report
Re: Are there labels in Pascal? Posted by Phat Nat on 24 Feb 2003 at 5:05 PM
: I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
:
: In basic, you would use:
:
: LabelX:
: //code
: if a = 5 then
: GOTO LabelX;
: end
: 

:
: If there are labels (Surely there are, right?) how would you use them?
:

Yep, you can. It's as easy as this:

USES Crt;
VAR
   Key : Char

LABEL VeryTop;

Begin
   VeryTop:
     WriteLn('Press a key...');
     ReadKey := Key;
     If UpCase(Key) <> 'Y' Then Goto VeryTop;
End.


Simple. Just decalre that before using them (Cannot be done in code section)

Phat Nat
Report
Labels in Pascal? - Don't Posted by Perran on 25 Feb 2003 at 5:20 AM
This message was edited by Moderator at 2003-2-25 7:10:38

: I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
:
: In basic, you would use:
:
: LabelX:
: //code
: if a = 5 then
: GOTO LabelX;
: end
: 

:
: If there are labels (Surely there are, right?) how would you use them?
:
Opius says in his post that labels help to create spaghetti code and that's correct. I would recommend learning to program without them. I started with Turbo Pascal 3.0 in 1984 and have never once used a label - goto. They're just not necessary. My$0.02
Report
Re: Labels in Pascal? - Don't Posted by Phat Nat on 25 Feb 2003 at 7:41 AM
: This message was edited by Moderator at 2003-2-25 7:10:38

: : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
: :
: : In basic, you would use:
: :
: : LabelX:
: : //code
: : if a = 5 then
: : GOTO LabelX;
: : end
: : 

: :
: : If there are labels (Surely there are, right?) how would you use them?
: :
: Opius says in his post that labels help to create spaghetti code and that's correct. I would recommend learning to program without them. I started with Turbo Pascal 3.0 in 1984 and have never once used a label - goto. They're just not necessary. My$0.02

Yes and No. They aren't the greatest way of doing loops and in general you are right, they should be avoided, However, I recently used a GOTO statement in an emulator that I was creating. Since Emulation depends on speed and REPEAT UNTIL / WHILE DO statements always do some sort of check Even if it's something like: REPEAT UNTIL 1 = 2. although these would never be equal, it still would check first. By using a GOTO, you can skip the check step. That's the only place I've ever used one however. My $0.02

Phat Nat




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.