LOTUSSCRIPT 言語


Exit ステートメントを使ってプロシージャを早期に終了する
Exit ステートメントは、実行がプロシージャ定義の最後またはブロックステートメントの最後に達する前に、プロシージャまたは Do、For、ForAll ステートメントの実行を終了します。

構文は次のとおりです。

Exit exitType

exitType は、キーワード Do、For、ForAll、Function、Sub、Property のいずれかである必要があります。

Do、For、ForAll ステートメントで Exit を使う場合、ブロックステートメントの最後の後に続く最初のステートメントから実行が続行されます。

次に例を示します。

' Compute the elapsed time to execute 1000 iterations
' of a simple Do loop.
' Time may vary, depending on the workstation.
Dim doCount As Integer, startTime As Single
startTime!= Timer()
doCount% = 0
Do
' Increment doCount% through 1000 iterations of the Do loop.
doCount% = doCount% + 1
If doCount% > 1000 Then Exit Do
Loop
' Come here upon exit from the Do loop.
Print Timer() - startTime!"seconds for 1000 iterations"
' Output:
' .109375 seconds for 1000 iterations

プロシージャで Exit を使う場合、プロシージャから正常に戻った場合と同様に実行が続行されます。

次の例は、サブルーチン内に前述の例の Do ステートメントを取り入れています。Exit Sub ステートメントは、doCount% が 1000 に達した後、サブルーチン ElapsedTime の実行を終了します。サブルーチン呼び出しの後の Print ステートメントから実行が続行されます。Do ループの実行を別に終了する必要はありません。Exit Sub ステートメントによって Do ループからサブルーチンの外に制御が移されます。

' Compute the elapsed time to execute a sub that runs
' 1000 iterations of a simple Do loop.
Public startTime As Single
Sub ElapsedTime
Dim doCount As Integer
doCount% = 0
Do
doCount% = doCount% + 1
If doCount% >= 1000 Then Exit Sub
Loop
' Because of the Exit Sub statement above, this Print statement
' will not be reached.
Print Timer() - startTime!, "seconds to run 1000 iterations"
End Sub
startTime!= Timer()
Call ElapsedTime()
Print Timer() - startTime!_
|seconds for sub call to run 1000 iterations|
' Output:
' .109375 seconds for sub call to run 1000 iterations

Exit For ステートメントが実行された後で実行が続行される場合、For ステートメントのカウント変数は最新の値になります。これは、For ステートメントの通常の終了後に実行が続行される場合と同じです。Exit ForAll ステートメントが実行された後で実行が続行される場合、ForAll 別名変数は未定義です。これは、ForAll ステートメントの通常の終了後に実行が続行される場合と同じです。

Exit Function ステートメントの実行の後、関数は呼び出し元に値を返します。通常に戻る場合と同様、これは終了前に割り当てられた最後の値です。何も割り当てられていない場合、関数の戻り値はその関数の初期値です。つまり、0、EMPTY (空)、または空の文字列 ("") が返されるか、何も返されません。次に例を示します。

Function TwoVerge(seqSeed As Integer) As Single
' Leave if the call argument is not a positive integer.
' The return value of TwoVerge is its initial value, 0.
If seqSeed% < 1 Then Exit Function
TwoVerge!= Sqr(seqSeed% + 1)
Dim i As Integer
For i% = 1 To seqSeed%
' TwoVerge computes and returns a value that must be
' 1 or greater, according to the following formula.
TwoVerge!= Sqr(1 + (seqSeed% + 1 - i%) * TwoVerge!)
Next i%
End Function

Print ステートメント内の TwoVerge への呼び出しは、結果を次のように示します。

Print "Seed:", -1, "Value:"TwoVerge(-1)
Print "Seed:", 20, "Value:"TwoVerge(20)
' Output:
' Seed:-1 Value:0
' Seed:20 Value: 1.999998

関連項目