Sub prog() Dim i As Integer Dim s As Long s = 0 For i = 1 To 10 If i Mod 2 = 0 Then s = s + i End If Next i MsgBox "sの値は" & s & "です" End Sub |
i = 1 (If 文の条件に当てはまらない) i = 2 s = s + 2 i = 3 (If 文の条件に当てはまらない) i = 4 s = s + 4 i = 5 (If 文の条件に当てはまらない) … i = 9 (If 文の条件に当てはまらない) i = 10 s = s + 10 i = 11 (→ i = 1 To 10 の範囲から外れたのでここで For 文終了) |
Sub prog() Dim i As Integer Dim s As Long s = 0 For i = 0 To 10 Step 2 s = s + i Next i MsgBox "sの値は" & s & "です" End Sub |
Sub prog() Dim i As Integer Dim s As Long s = 0 i = 0 While i <= 10 s = s + i i = i + 2 Wend MsgBox "sの値は" & s & "です" End Sub |
While (条件) (命令) Wend |
Ctrl + Pause (2 つのキーの同時押し) |
i = 0 (ここから While 文に入る) i = 0 は i<=10 を満たす s = s + 0 i = i + 2 (i=2) i = 2 は i<=10 を満たす s = s + 2 i = i + 2 (i=4) i = 4 は i<=10 を満たす s = s + 4 i = i + 2 (i=6) … i = 10 は i<=10 を満たす s = s + 10 i = i + 2 (i=12) i = 12 は i<=10 を満たさない (While 文終了) |