LOTUSSCRIPT 言語


GoTo ステートメントを使用して制御を移す
分岐ステートメントの GoTo は、制御を無条件に移します。

構文は次の通りです。

GoTo label

このステートメントが実行されると、ラベル label の付いたステートメントに制御が移ります。GoTo とそのジャンプ先は同一プロシージャ内になければなりません。制御のフローは実行時に決まります。

次の例は、数値が π (pi) にどれだけ近似しているかを調べるサブルーチン内で、Goto ステートメントを使用して適切に制御を移します。ユーザーが π の値を推測して特定の桁数だけ入力し、スクリプトが値を調べて報告します。

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:")))

この例で GoTo を使用して制御を移す効果は、入力された値が 「Close but not very」であるかどうかを調べる If ステートメントをスキップすることにあります。「Very close」 であると分かれば、さらに調べる必要はありません。

次の例では、GoTo を使用して、一連の計算 (.25 ^ .25、.25 ^ (.25 ^ .25)、.25 ^ (.25 ^ (.25 ^ .25))、以下同様) を 2 つの連続した式が互いに .0001 以内に入るか 40 個の式が計算されるまで繰り返します。

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

この例は、(.25 ^ .25、.25 ^ (.25 ^ .25)、.25 ^ (.25 ^ (.25 ^ .25))、以下同様) の代わりに、ゼロと 1 の間の任意の値 x について一連の値 (x ^ x、x ^ (x ^ x)、以下同様) の計算を汎用化できます。

関連項目