Ada and goto
Every language has its hidden βfeaturesβ, and Ada, like most other languages has a goto statement. Any statement could be labelled by an identifier enclosed in double angle brackets, << >>. For example:
<<gohere>> g = 12.3; ... goto gohere;
However, the goto in Ada is somewhat better behaved. It cannot transfer control outside the current subprogram or package body; it cannot transfer control inside a structure (e.g. from else to then in an if statement); and it cannot transfer control from the outside of a structured statement into the body of a structured statement. Consider the following code:
with ada.Text_IO; use Ada.Text_IO; with ada.Integer_Text_IO; use ada.Integer_Text_IO; procedure gotoada is x,y,z : integer; begin put_line("Enter a number: "); get(x); <<lbl1>> if x = 1 then y := 1; z := 1; <<lbl2>> else y := 2; z := 2; end if; case y is when 1 => put_line("jump to top"); goto lbl1; when others => put_line("jump to middle"); goto lbl2; end case; end gotoada;
The goto associated with lbl1 is allowed. However the goto associated with lbl2, which jumps into the middle of the if statement above it, Β is not allowed. Try to compile this, and youβll get the following compiler message:
gotoada.adb:24:21: target of goto statement is not reachable
Languages like Pascal would easily allow this sort of code, but Ada was designed to prevent issues with uber unstructured code.
Β
spqr