Transferring control with the GoTo statement
The branching statement GoTo transfers control unconditionally.
The syntax is:
GoTo label
When this statement is executed, LotusScript® transfers control to the statement labeled label. GoTo and its target must be in the same procedure. The flow of control is determined at run time.
This example uses a GoTo statement to transfer control appropriately within a sub that checks how closely a number approximates pi. A user types a guess at the value of pi to some number of digits, and the script checks the value and reports on it.
Sub ApproxPi(partPi As Double)
Dim reportMsg As String
' See how good the approximation is,
' and assign a response message.
reportMsg$ = "Not close at all"
If Abs(PI - partPi#) < 1E-12 Then
reportMsg$ = "Very close"
GoTo MsgDone
End If
If Abs(PI - partPi#) < 1E-6 Then reportMsg$ = _
"Close but not very"
' Print the message and leave.
MsgDone: MessageBox(reportMsg$)
End Sub
' Ask the user to guess at PI; then call ApproxPi, and report.
Call ApproxPi(CDbl(InputBox$("A piece of PI, please:")))
The effect of the transfer using GoTo in the example is to skip the If statement that checks whether the supplied approximation is "Close but not very." If it's already known to be "Very close," it makes no sense to check further.
This example uses GoTo to iterate through the sequence of calculations .25 ^ .25, .25 ^ (.25 ^ .25), .25 ^ (.25 ^ (.25 ^ .25)), and so on, until either two successive expressions in this sequence are within .0001 of each other, or 40 expressions have been calculated.
Sub PowerSeq
Dim approx As Single, tempProx As Single, iters As Integer
approx! = .25
iters% = 1
ReIter:
tempProx! = approx!
approx! = .25 ^ tempProx!
If Abs(tempProx! - approx!) >= .0001 And iters% < 40 Then
' Iterate again.
iters% = iters% + 1
GoTo ReIter
End If
Print approx!, Abs(approx! - tempProx!), "Iterations:" iters%
End Sub
Call PowerSeq()
' Output:
' .5000286 6.973743E-05 Iterations: 25
The example can be generalized to calculate the sequence of values x ^ x, x ^ (x ^ x), and so on, for any value x between 0 and 1, instead of .25 ^ .25, .25 ^ (.25 ^ .25), and so on.