User Tools

Site Tools


k10:k10.5:k10.5.5:start

10.5.5 GoTo

The following four chapters describe control structures, the use of which is controversially discussed:

  • GoTo Unconditional jump to a destination (-Label).
  • GoSub Call of a local subroutine.
  • On GoTo Calculated unconditional jump to a selected target (-Label).
  • On GoSub Calculated call of local subroutines.

As can be shown in the examples under consideration, there are many applications in which the above four statements can be replaced by other control structures. Or put it another way: The use of the four above statements is avoidable if you don't fear the complexity of the Gambas code, for example, to leave a triple For control structure in an orderly fashion under a particular condition.

10.5.5.1 GoTo syntax

The system switches to a target (-label) that is declared elsewhere in a procedure or function:

GoTo Label
…
Label:
  <Instruction(s)>

10.5.5.2 Notes on syntax

  • Label points here to a target for the GoTo statement. The target specification ends with a colon. You can use capital letters or a preceding underscore to make your destination (-label) stand out in a special way.
  • A label is always local to a function or procedure.
  • 'Label' in connection with the instruction' GoTo Label' should not be confused with the class label? chapter 16.1 Label!
  • GoTo and Label can be used to exit a control structure.However, GoTo and Label cannot be used to enter a control structure.

The GoTo in connection with the above statement must be distinguished from the method Editor. Goto (…) of the component Editor (gb. qt4. ext):

Editor1.GoTo(0, String.Len(Editor1.Lines[0].Text)) ' Place cursor at the end of the 1st line
Editor1.GoTo(Editor1.Lines.Count, String.Len(Editor1.Text)) ' Place cursor at end of text

The examples in the next section mainly show source code excerpts and are commented adequately.

10.5.5.3 Example 1

The statement' GoTo Label' is sometimes acceptable as ultima ratio. This is exactly when you want to leave two nested loops:

variant 1 - without using the' GoTo Label' instruction

Dim i, j As Integer 
Dim bBreak As Boolean 
 
For i = 0 To 10 
  For j = 0 To 10 
    ' The double loop should be exited as soon as i*j > 20
    If (i * j > 20) Then 
      bBreak = True 
      Break 
    Endif 
  Next ' j
  If bBreak = True Then Break 
Next ' i

You can only jump from the innermost loop with' Break'. Therefore, you must set a variable that also signals to the outer loop that it should also be exited. * The variable' bBreak' is only true if you jumped out of the inner loop and leave the outer loop. You have to decide for yourself whether the above source code is easy to read.

Variant 2 - Use of the' GoTo Label' instruction

Dim i, j As Integer 
 
For i = 0 To 10 
  For j = 0 To 10 
    If i * j > 20 Then GoTo LEAVEBOTH 
  Next 
Next 
 
LEAVEBOTH: 
<Instruction(s)>
'  more...

10.5.5.4 Example 2

An example of a task in which the GoTo statement can demonstrate its strength is the search in an N-dimensional array for a certain integer value - here in a two-dimensional integer[][][]-array.

Dim iN, iI, iX As Integer 
 
For iN = 0 To aArray.Max 
  For iI = 0 To aArray[iN].Max 
    If iX = aArray[iN][iI] Then GoTo _Found 
  Next 
Next 
 
_Found: 
' Here you can use iN and iI. The element was found if iN <= aArray. Max

Bild 1

Figure 10.5.5.4.1: Searching for an (integer) number in a 2-dimensional array

The complete project for example 2 can be found in the download area.

The only way in example 2 to dispense with the GoTo statement to jump from two nested For control structures when a certain condition is fulfilled is to use a flag:

Dim iN, iI As Integer 
Dim bBreak As Boolean = False 
 
For iN = 0 To aArray.Max 
  For iI = 0 To Array[iN].Max 
    If iX = aArray[iN][iI] Then 
      bBreak = True 
      Break 
    Endif 
  Next 
  If bBreak Then Break 
Next 

In my opinion, however, this is not a good solution because it is less legible and less efficient. For several nested loops, it is definitely more complicated. A similar problem occurs if, for example, you want to iterate over an N-dimensional array and N is only known at runtime. Since a For control structure can only be counted linearly, you need N nested For control structures. If N is not known at runtime, a dynamically allocated array of iterators can be used to build an N-dimensional For control structure using GoTo. This is required, for example, when generating all possible character strings with N characters from a certain alphabet, where N is selected by the user - as it is done with' brute forcing'.

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k10/k10.5/k10.5.5/start.txt · Last modified: 23.09.2023 by honsek

Page Tools