DO循环的四种形式
Do                    do                  do while 条件      do until 条件
语句行 
Loop while 条件        loop until 条件        loop                loop
WHILE 条件为真时 继续运行循环程序
Until 条件为真时, 退出程序
Private Sub Command0_Click()
Rem 在程序运行时提示用户输入1-12的数字,程序判断且输出对应数字所属
Rem 月份对应的季节
Dim k As Integer, jx As Integer
Do
k = InputBox("请输入1-12的数字", "提示", 1)
Select Case k
Case 3 To 5
  MsgBox "春季", vbOKOnly, "季节输出"
Case 6 To 8
MsgBox "夏季", vbOKOnly, "季节输出"
Case 9 To 11
MsgBox "秋季"
Case 1, 2, 12
MsgBox "冬季", vbOKOnly, "季节输出"
Case Else
MsgBox "您输入的数据有误!"
End Select
jx = MsgBox("是否继续判断?", vbYesNo, "提示")
Loop Until jx = 7
End Sub
计算 1100中所有数据的和
Dim k As Integer, s As Integer '0
k = 1: s = 0
Do Until k > 100
s = s + k
k = k + 1
Loop
MsgBox "1-100的和为" & s
End Sub
Dim k As Integer, s As Integer '0
k = 1: s = 0
Do while k <=100
s = s + k
k = k + 1
Loop
MsgBox "1-100的和为" & s
End Sub
Dim k As Integer, s As Integer '0
For k = 1 To 100 Step 1
s = s + k
Next k
MsgBox "1-100的和为" & s
Private Sub Command0_Click()
Dim k As Integer, s As Integer '0
For k = 1 To 20 Step 3 '1    4    7  10    13  16  19  22do while语句流程图
s = s + k              '1    5    12  22    35  51  70
Next k                '4    7    10  13    16  19  22
MsgBox "和为" & s
MsgBox "k的值为" & k
End Sub
Private Sub Command0_Click()
Dim h As Integer, k As Integer
h = 1: k = 1
For h = 1 To 9
Debug.Print
Debug.Print h & ")";
For k = 1 To h
  Debug.Print h * k;
Next k
Next h
End Sub
一维数组的赋值
Option Compare Database
Private Sub Command0_Click()
Dim ar1(10) As Integer
Dim k As Integer
For k = 0 To 10
ar1(k) = InputBox("请输入第" & k + 1 & "个元素的值")
Debug.Print ar1(k);
Next k
End Sub
双层FOR循环给数组赋值
Option Compare Database
Option Base 1
Private Sub Command0_Click()
Dim ar1(3, 4) As Integer
Dim k As Integer, h As Integer
For k = 1 To 3
  For h = 1 To 4
      ar1(k, h) = InputBox("请输入第" & k & "行第" & h & "列元素的值")
  Debug.Print ar1(k, h);
  Next h
Debug.Print
Next k
End Sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。