Are there labels in Pascal? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Are there labels in Pascal?

jim_giboneyjim_giboney Posts: 56Member
I know you can use Labels in Basic, and I think C++, but can you use them in pascal?

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

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

Comments

  • OpiusOpius Posts: 14Member
    [b][red]This message was edited by Opius at 2003-2-24 16:15:50[/red][/b][hr]
    : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
    :
    : In basic, you would use:
    : [code]
    : LabelX:
    : //code
    : if a = 5 then
    : GOTO LabelX;
    : end
    : [/code]
    :
    : 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

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

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

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

    -Opius


  • Phat NatPhat Nat Posts: 757Member
    : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
    :
    : In basic, you would use:
    : [code]
    : LabelX:
    : //code
    : if a = 5 then
    : GOTO LabelX;
    : end
    : [/code]
    :
    : If there are labels (Surely there are, right?) how would you use them?
    :

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

    [code]
    USES Crt;
    VAR
    Key : Char

    LABEL VeryTop;

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

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

    Phat Nat
  • ManningManning Posts: 1,621Member
    : [b][red]This message was edited by Opius at 2003-2-24 16:15:50[/red][/b][hr]
    : : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
    : :
    : : In basic, you would use:
    : : [code]
    : : LabelX:
    : : //code
    : : if a = 5 then
    : : GOTO LabelX;
    : : end
    : : [/code]
    : :
    : : 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)

    [code]
    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;
    [/code]
  • PerranPerran Posts: 241Member
    [b][red]This message was edited by Moderator at 2003-2-25 7:10:38[/red][/b][hr]
    : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
    :
    : In basic, you would use:
    : [code]
    : LabelX:
    : //code
    : if a = 5 then
    : GOTO LabelX;
    : end
    : [/code]
    :
    : 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
  • Phat NatPhat Nat Posts: 757Member
    : [b][red]This message was edited by Moderator at 2003-2-25 7:10:38[/red][/b][hr]
    : : I know you can use Labels in Basic, and I think C++, but can you use them in pascal?
    : :
    : : In basic, you would use:
    : : [code]
    : : LabelX:
    : : //code
    : : if a = 5 then
    : : GOTO LabelX;
    : : end
    : : [/code]
    : :
    : : 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

Sign In or Register to comment.