DO循环的四种形式
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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
计算1-100中所有数据的和
Dim k As Integer, s As Integer '0
k = 1: s = 0
Do Unti l 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 22 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