3.13 The goto Statement

[ Table of Contents ] Prev ] Chapter Overview ] Next ] [ Glossary/Index ]

The goto statement is a simple statement used to transfer control to a specified, "labeled" location in the same executable part. It includes the reserved word, goto.

Form of the goto statement

goto Label_Name;

Form of the label

<< Label_Name>>

The use of the goto statement is generally frowned upon by many Ada programmers. (See the famous paper, "Go To Statement Considered Harmful" [Dijkstra68].) However, goto was included in Ada partly because it could be useful for the automatic translation of code in another language (such as Fortran) to Ada. Furthermore, some programmers feel that goto's provide a nice way to create certain structures, such as state machines. The example program given below illustrates the use of goto's in implementing a state machine.

Im3-14a.gif (1946 bytes)

This diagram is a UML state diagram, used to depict states and state transitions. We imagine a frog jumping from one lily pad to another. The frog always starts a session on Pad_1. On Pad_2 it "flips a coin" to determine which pad to jump to next. On Pad_4 the frog may decide to quit, based on the elapsed time since the start of its jumping session.

Im3-14b.gif (4132 bytes)

The Jumping_Frog package is an ADO that exports the Do_Jumps procedure. The body of the package creates a generic instance, Coin_Flipper, of the predefined package, Ada.Numerics.Discrete_Random, a generic package -- in order to simulate the coin-flipping action used when the frog is on Pad_2.

The Do_Jumps procedure has two parameters, which specify the Resting_Time on each pad and the Quitting_Time.

The Coin_Flipper package and related variables are adapted from the example given in paragraph A.5.2(58) of [ARM95], making use of the random number generation capability provided by the Discrete_Random package, a grandchild of package Ada.

Source Code Listing

------------------------- Jumping_Frog  --------------------------
--  This package implements a 4-state abstract data object. It 
--  creates a coin-flipping random number generator package to 
--  help decide which transition to make in the "Pad_2" state. 
------------------------------------------------------------------
package Jumping_Frog is
  procedure Do_Jumps(Resting_Time  : Duration;
                     Quitting_Time : Duration);
end Jumping_Frog;
-----------------------------------------------
with Ada.Text_IO, Ada.Calendar;
use  Ada.Text_IO, Ada.Calendar;
with Ada.Numerics.Discrete_Random;
package body Jumping_Frog is
    
  Starting_Time : Time;
  Current_Time  : Time;                   
  Session_Time  : Duration;
  
  type Coin  is (Heads, Tails);
  package Coin_Flipper is new 
          Ada.Numerics.Discrete_Random(Coin); -- generic instance
  use Coin_Flipper;                           -- use clause
  G    : Generator;
  Toss : Coin; 
  -----------------------------------------------
  procedure Do_Jumps(Resting_Time  : Duration;
                     Quitting_Time : Duration) is
    Count : Integer := 0;
  begin
    Starting_Time := Clock;
    Put_Line("Start session on Pad_1");
    
    << Pad_1 >>
      delay Resting_Time;
      Put_Line("  Jump to Pad_2");
      
    << Pad_2 >>
      delay Resting_Time;
      Reset(G);
      Toss := Random(G);                      -- coin toss
      if Toss = Heads then
        Put_Line("  Jump to Pad_3");
        goto Pad_3;                           -- goto statement
      else
        Put_Line("  Jump to Pad_4");
        goto Pad_4;                           -- goto statement
      end if;  
      
    << Pad_3 >>
      delay Resting_Time;
      Put_Line("  Jump to Pad_4");
      
    << Pad_4 >>
      delay Resting_Time;
      Current_Time := Clock; 
      Session_Time := Current_Time - Starting_Time;
      if Session_Time < Quitting_Time then
        Put_Line("  Jump to Pad_1");
        goto Pad_1;                           -- goto statement
      end if;
      
      Put_Line("Quit session");  
  end Do_Jumps;
end Jumping_Frog;
------------------------------------------------------------------
--------------------------- Test_Frog ----------------------------
--  This test procedure calls the Do_Jumps procedure in the 
--  Jumping_Frog package, with two parameters. 
------------------------------------------------------------------
with Jumping_Frog;
procedure Test_Frog is
begin
  Jumping_Frog.Do_Jumps(Resting_Time  => 2.0,
                        Quitting_Time => 20.0);                        
end Test_Frog;
------------------------------------------------------------------

Note that the << Pad_2 >> label is never used, but by including it we have set up the framework for a more complex set of transition rules.

An example output from the above program follows:

     Start session on Pad_1
       Jump to Pad_2
       Jump to Pad_3
       Jump to Pad_4
       Jump to Pad_1
       Jump to Pad_2
       Jump to Pad_4
       Jump to Pad_1
       Jump to Pad_2
       Jump to Pad_3
       Jump to Pad_4
     Quit session

Related Topics

2.9 Simple (ADO) Stack A.2 Simple and Compound Statements
B.2 Package Ada and Children

[ Back to top of pagePrev ] Next ]