GOTO

<< Click to Display Table of Contents >>

Navigation:  Gekko User Manual > Gekko statements >

GOTO

Previous pageReturn to chapter overviewNext page

GOTO can be used to transfer execution to some other point (TARGET) in the program.

 

You should mostly use this statement to jump out of loops (cf. the example below). It is not intended for jumping around in plain non-looping code, where the presence of GOTO/TARGET may render the programs slow-running and hard to read.

 

 


 

Syntax

 

goto name;

 

The label must be name-like, that is, alphanumeric characters including underscore (and not starting with a digit). You can not use scalars or expressions etc. as labels.

 

 


 

Examples

 

%sum = 0; 
for val %= 1 to 5; 
  if(%== 4);
    goto lbl1; 
  end; 
  %sum += %i; 
end; 
target lbl1;

 

This example skips the iterations before the fourth iteration is about to be executed. The value of %sum will be 6 (= 1+2+3, not 1+2+3+4+5).

 

The example below is NOT what the statement is intended for:

 

tell 'a'; goto x1; 
target x2; tell 'c'; goto x3;
target x1; tell 'b'; goto x2; 
target x3; tell 'd';

 

This prints 'a', 'b', 'c', 'd', but please use other means to organize the flow of your gcm-file!

 

 


 

Note

 

Target names cannot be duplicated. An error will be issued.

 

The program will also fail with an error, if the label does not exist. But 'orphaned' labels are accepted (a TARGET without a corresponding GOTO).

 

You cannot call a target inside a loop, from outside the same loop. For instance, the following will fail, and an error will be issued:

 

%sum = 0; 
goto lbl1;
for val %= 1 to 5; 
  target lbl1;
  %sum += %i; 
end; 

 

Eternal loops may be accidently created, for instance the line target lbl1; goto lbl1; will run forever. This example is easy to spot, but such problems may arise if the GOTO structure is misused. It has been proven that the GOTO statement is technically superflous, and it can lead to so-called spaghetti code (cf. Dijkstra: "Go To Statement Considered Harmful").

 

At a later point, BREAK and CONTINUE might be added to Gekko loops, too.

 

 


 

Related statements

 

TARGET