LOTUSSCRIPT 言語
構文は次の通りです。
If condition Then
実際のステートメント内の改行は、構文に示すとおりでなければならず、If 節、ElseIf 節、Else 節の内容は正しい順序で記述しなければなりません。
実行されるのは、TRUE として評価された最初の条件に続くステートメントグループか、Else キーワードに続くステートメントグループのどちらか 1 つです(TRUE に評価される条件がなく Else 節もない場合、ステートメントは実行されません)。ステートメントグループが実行されると、それ以降の条件式は評価されません。したがって、ElseIf 節の順序は重要です。プログラムは End If キーワードの次のステートメントから再開されます。
別のステートメントには含まれていない If...Then...ElseIf ステートメントは、制御を移すことによってのみ実行中にスキップできます。制御を移すには、Exit か End ステートメントを使用するか、または Goto、GoSub、ラベルなどを使用してラベル付きステートメントへ移動します。これらのすべてのステートメントは、プロシージャの一部でなければなりません。
次の例では、If..Then...ElseIf を使用して Timer の値が Morning、Afternoon、Evening のどれを表しているかを判断します。
Dim timeTest As Single timeTest!= Timer() ' The Timer function returns ' the number of seconds elapsed ' since midnight. If timeTest!< 43200 Then Print "Morning" ElseIf timeTest!< 64800 Then Print "Afternoon" Else Print "Evening" End If
If 節と ElseIf 節の内容の順序を変えると、間違った結果になります。たとえば、Timer() の値 38017 は午前中の時刻ですが、この例では「Afternoon」と表示されます。
Dim timeTest As Single timeTest!= Timer() ' The Timer function returns ' the number of seconds elapsed ' since midnight. If timeTest!< 64800 Then Print "Afternoon" ElseIf timeTest!< 43200 Then Print "Morning" Else Print "Evening" End If
次の例は、If..Then...ElseIf ステートメントを使用し、ユーザーが提供した整数に適切な英語の接尾辞 (1 には 「st」、17 には 「th」 など) を追加して序数に変更する処理を示しています。スクリプトは 0 から 50 の範囲外の数値と、小数点以下部分のある数値では、異なる処理を行います。If...Then...ElseIf のネストレベルが 3 つあります。各ステートメントには、それぞれ独自の End If 句が必要です。End If 句は実行中の最も内側のステートメントを終了します。
Dim anInt As String, lastDigit As String, printNum As String anInt$ = InputBox$("Enter a whole number between 0 and 50:") ' Test for a number; print message if not, and do nothing more. If Not IsNumeric(anInt$) Then MessageBox("That's not a number.") ' Test for whole number; print message if not, ' and do nothing more. ElseIf Fraction(CSng(anInt$)) <> 0 Then MessageBox("That's not a whole number.") Else ' Test for number within required range. If CInt(anInt$) <= 50 And CInt(anInt$) >= 0 Then ' Number is within range.Find and append ' the correct suffix. lastDigit$ = Right$(anInt$, 1) If lastDigit$ = "1" And anInt$ <> "11" Then printNum$ = anInt$ & "st" ElseIf lastDigit$ = "2" And anInt$ <> "12" Then printNum$ = anInt$ & "nd" ElseIf lastDigit$ = "3" And anInt$ <> "13" Then printNum$ = anInt$ & "rd" Else printNum$ = anInt$ & "th" End If ' Print the ordinal in a message box. MessageBox("This is the " & printNum$ & " number.") Else ' Number is out of range.Print message, ' and do nothing more. MessageBox("That number's out of range.") End If End If ' Output: ' (For user input 3):"This is the 3rd number." ' (For user input -5.1):"That's not a whole number." ' (For user input 51):"That number's out of range." ' (For user input abacus):"That's not a number."
この例は、条件処理が 3 レベルまで深くネストされていなければ、もっと読みやすくなります。このスクリプトの主要ロジックを 1 つのプロシージャに含めれば、もっと簡単に書き換えることができます。
関連項目