計算 1+2+3+...+100 之值,請分別用 For,While、Until,Do Loop方式為之
(Excel VBA參考程式碼如下)
Sub TestSummation()
Dim iiNum As Integer
Dim iiSum As Integer
iiSum = 0
iiNum = 0
For iiNum = 1 To 100
iiSum = iiSum + iiNum
Next iiNum
Debug.Print iiSum
'************************(1)
iiSum = 0
iiNum = 0
Do While iiNum = 100
iiSum = iiSum + iiNum
iiNum = iiNum + 1
Loop
Debug.Print iiSum
'************************(2)
iiSum = 0
iiNum = 0
Do
iiSum = iiSum + iiNum
iiNum = iiNum + 1
Loop While iiNum = 100
Debug.Print iiSum
'************************(3)
iiSum = 0
iiNum = 0
Do Until iiNum 100
iiSum = iiSum + iiNum
iiNum = iiNum + 1
Loop
Debug.Print iiSum
'************************(4)
iiSum = 0
iiNum = 0
Do
iiSum = iiSum + iiNum
iiNum = iiNum + 1
Loop Until iiNum 100
Debug.Print iiSum
'************************(5)
End Sub