The following four chapters describe control structures, the use of which is controversially discussed:
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.
The system switches to a target (-label) that is declared elsewhere in a procedure or function:
GoTo Label … Label: <Instruction(s)>
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.
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...
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
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'.