Last Updated:

Goto to Pascal transition operator

 
Pascal transition operator

Mark the operator

label1: operator

Labels are described BEFORE variables and constants are declared using the label service word.

label 1,2,3;
var
num: integer;
begin
...

Rules of use goto:

  • One label – one operator;
  • The label can be an integer from 0 to 9999 or an identifier no longer than 63 characters;
  • Multiple labels are separated by a comma;
  • goto does not work if the label is not associated with the code block in which it is applied or does not refer to anything at all;
  • Currently, it is almost not used.

Example No1. Output a number with a condition

label 1,2;
var i := 5;

begin
2: if i < 0 then goto 1;
write(i);
Dec(i);
goto 2;
1:
end.

Program output 543210

Example No2. Simple loop with skipping iteration

program exGoto;
label 1;
var
a : integer;

begin
a := 10;
(* the beginning of the main cycle *)
1: repeat
if( a = 15) then

begin
(* skip iteration when a = 15*)
a := a + 1;
goto 1;
end;

writeln('value a: ', a);
a:= a +1;
until a = 20;
end.

The program will output lines from 10 to 19, while skipping 15.

Break and continue procedures

 

Procedures that interrupt for, while, and repeat loops. Break is used when you want to end a loop early. In this case, the statement that follows the loop is executed. Continue completes the current iteration of the loop, moving to the end of the loop.

Example with both commands

flag := False;
for var i:=1 to 10 do
begin
read(x);
if x<0 then continue; skipping the current iteration of
the loop if x=5 then
begin
flag := True;
break; Exit the end loop
.
end;

Both procedures are used only within the loop.

 

Exit Termination Statement

 

Allows you to instantly exit the current function. At the same time, if it is used in the body of the main program, it will be completely completed.

function Analyze(x: integer): boolean;
begin
if x<0 then
begin
Result := False;
exit
end;
...
end;

Conclusion

 

Currently, programmers advise not to use goto in their programs. At the dawn of programming, goto allowed nonlinear access to the elements of the program, and now it is not necessary due to more advanced designs.

Using goto creates confusion, disrupts execution logic, and makes the code less efficient. In modern projects, you will not meet this operator by 99%.