LOTUSSCRIPT 言語
構文は次の通りです。
For countVar = first To last [ Step increment ]
次の例は、Step または Next オプションアイテムを使用しない For ステートメントを示します。
Dim power2 As Integer For iV = 1 To 15 power2 = 2 ^ iV - 1 Print power2% ; Next ' Output: ' 1 3 7 15 31 63 127 255 511 1023 2047 4095 8191 16383 32767
上の例の For ステートメントの最初の行は、次と同等です。
For iV = 1 To 15 Step 1
つまり、Step increment 句がステートメントから省略された場合、increment の既定値は 1 です。
For ステートメントの本文には何も記述しなくてもかまいません。For と Next の間にステートメントは必要ありません。
制御式内の変数: そのデータ型および宣言
制御式 first、last、または increment に変数が現れた場合、LotusScript はその現在値を使用します。前に宣言または使用されていない場合、LotusScript はこれを暗黙のうちに Variant 型の変数として宣言し、EMPTY に初期化します。For ステートメントを実行する前に、これらの式の変数が宣言されていることを確認する必要があります。
LotusScript は、For ステートメントに入った時点でカウント変数を first の値に初期化します。countVar が前に宣言および使用されていない場合、LotusScript はこれを Variant 型として宣言します(スクリプトに Option Declare ステートメントが含まれている場合は、countVar は For ステートメントで使用する前に宣言する必要があることに注意してください)。ループ変数は常に宣言する必要があります。タイトなループで値を Variant 型に変換するには、コンピュータリソースがさらに必要となります。
以下に例を示します。
' If the variable iV was not previously declared or used, ' this For statement declares it as a Variant. ' Its value after the For statement completes execution is the ' last value assigned to it during the For statement ' execution (16). For iV = 1 To 15 Next Print TypeName(iV), iV iV = "abc" Print TypeName(iV), iV ' Output: ' INTEGER 16 ' STRING abc
次の例では、For ステートメントの Integer 型のカウント変数の上限値として 2 ^ 15 を使用しようとした結果、コンパイラエラーが発生します。この理由は、LotusScript の Integer 型の最大値は、(2 ^ 15) - 1 であるためです。
Dim i As Integer For i% = 1 To 2 ^ 15 Next ' Output: ' Error 6:O008 オーバーフローしました
カウント変数が Variant 型である場合、LotusScript は For ステートメントの実行時にこの値を適切なデータ型に変換します。
For iV = 1 To 2 ^ 15 Next Print TypeName(iV), iV ' Output: ' LONG 32769
次の例と似ています。
' The Variant kV has a Double value in every iteration of ' this loop, because the For statement first assigns it ' the Double value 1.0 and thereafter adds 1 to the value ' in each iteration. For kV = 1.0 To 3 Next Print TypeName(kV), kV ' Output: ' DOUBLE 4
次の例では、For ステートメントを 2 回目に実行している間、kV の値は、Double 型の値 2.1 になっています。
' This loop iterates only twice because the third value ' of kV is 3.2, which is larger than the limiting value, 3. For kV = 1 To 3 Step 1.1 Print TypeName(kV), kV Next ' Output: ' INTEGER 1 ' DOUBLE 2.1
カウント変数には、LotusScript のデータ型の変換規則が適用されます。
' In this instance, the Step value, 1.1, is rounded to the ' Integer value 1 each time it is used to increment k%, ' because k% is declared as an Integer variable. Dim k As Integer For k% = 1 To 3 Step 1.1 Print TypeName(k%), k% Next ' Output: ' INTEGER 1 ' INTEGER 2 ' INTEGER 3
ネストされた For ステートメント
次の例では、ネストされた For ステートメントの有用性を示します。この例は、任意の正の整数 n に対して、1 から n までのすべての整数 k についての 2 項係数 (数学的に b(j; k) で示されます) を計算し、出力します。パスカルの三角形の方式をアルゴリズムとして使用し、b(j; k) は b(j - 1; k - 1) + b(j - 1; k) の合計として計算されます。
次の例では、3 つの別個の For ステートメントが一番外側の For ステートメントの内部にネストされています。
Sub CoArray(n As Integer) Dim i As Integer, j As Integer, k As Integer Dim coHold() As Double, coCalc() As Double ' Initialize arrays coHold and coCalc to 0. ' Alternate elements within each of these arrays will ' always be 0. The coefficients are stored in coCalc by ' addition from coHold. ReDim coHold(2 * n%) ReDim coCalc(2 * n% + 1)
coHold(n%) = 1 Print "Binomial coefficients for the integers up to:" n%
' Each iteration of this outer For statement "For j% ..." ' computes a line of coefficients. For j% = 0 To n% If j% > 0 Then ' The statement "For k%..." creates an array ' of coefficients in the middle of array coCalc. ' Alternate elements in this part of coCalc ' remain 0, and the ends of coCalc remain 0. For k% = n% - j% + 1 To n% + j% - 1 coCalc(k%) = coHold(k% - 1) + coHold(k% + 1) Next k% End If ' Set the 0-th and j-th coefficients to 1. coCalc(n% - j%) = 1 coCalc(n% + j%) = 1
Print Print "Coefficients for j = "j%":"; ' The statement "For k% ..." writes the new coefficients ' back into coHold to be used the next time around. For k% = n% - j% To n% + j% coHold(k%) = coCalc(k%) Next k% ' This For statement prints the line of coefficients for ' this value of j%.Every 2nd element of coCalc is 0. ' Don't print 0's. For k% = 0 To 2 * n% If coCalc(k%) > 0 Then Print coCalc(k%); Next k% Next j% End Sub
Call CoArray(5)
'Output: ' Binomial coefficients for the integers up to:5 ' Coefficients for 0 :1 ' Coefficients for 1 :1 1 ' Coefficients for 2 :1 2 1 ' Coefficients for 3 :1 3 3 1 ' Coefficients for 4 :1 4 6 4 1 ' Coefficients for 5 : 1 5 10 10 5 1
引数により大きな値を使用してサブルーチン CoArray を呼び出せば、また別の 2 項係数の集合を取得できます。
このアルゴリズムのその他の機能は、次のとおりです。
FactNum(n%) / (FactNum(k%) * FactNum(n% - k%))
こちらのほうが、より一般的な係数の計算方法です。FactNum を使用してルーチンを作成し、前述の例のサブルーチン CoArray で作成された係数の組と同じものを計算し、出力できます。FactNum 自身は、For ステートメントを使用する関数として次のように記述できます。
Function FactNum(n As Integer) As Double FactNum# = 1 For i% = 1 To n% FactNum# = FactNum# * i% Next i% End Function
それぞれの方法の利点は、次のとおりです。
Next k% Next j%
つまり、k% と j% は、これらのステートメントでは省略できます。次のように記述しても同様です。
Next k%, j%
この構成を使用する場合、カウンタ値を並べる順序が正しくなければなりません。この順序は、内部にある For ステートメントのカウンタから外側のステートメントのカウンタへの順序です。
For ステートメントの一般的なエラー
次の状況は、For ステートメントを作成する際の論理エラーをいくつか示し、LotusScript がそれにどう応答するかを示します。
For i% = 1 To 3 For j% = 1 To 2 Next i% Next j% ' Output: ' Error 53:N001 名前は FOR の制御変数と一致していません:I
For i% = 1 To 3 Do Print "test" Next Loop ' Output: ' Error 1:Unexpected:NEXT; Expected:LOOP
For i% = 1 To 3 For i% = 1 To 3 Next Next ' Output: ' Error 52:F002 FOR のカウント変数はすでに使用されています:I