1 / 15

第 7 章 VBA 流程控制结构

第 7 章 VBA 流程控制结构. 主讲人:辛慧杰 (21 号) 单位:大连民族学院. ACCESS 数据库程序设计. 顺序结构. 流程控制结构. 选择结构. 循环结构. 如何求 1+2+3+ …… +100 ?. 第 1 步 :先求 1 加 2 ,得到结果 3 第 2 步 :将 3 加 3 ,得到结果 6 第 3 步 :将 6 加 4 ,得到结果 10 第 99 步 :将 ? 加 100 ,得到结果 ?. ……. sum=0 sum=sum+1 sum=sum+2 sum=sum+3 sum=sum+100. sum=1+2 sum=sum+3

xylia
Download Presentation

第 7 章 VBA 流程控制结构

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第7章 VBA流程控制结构 主讲人:辛慧杰(21号) 单位:大连民族学院 ACCESS数据库程序设计

  2. 顺序结构 流程控制结构 选择结构 循环结构

  3. 如何求1+2+3+……+100? 第1步:先求1加2,得到结果3 第2步:将3加3,得到结果6 第3步:将6加4,得到结果10 第99步:将?加100,得到结果? …… sum=0 sum=sum+1 sum=sum+2 sum=sum+3 sum=sum+100 sum=1+2 sum=sum+3 sum=sum+4 sum=sum+100 1 2 3 100 sum=sum+i …… i=i+1 ……

  4. 循环的本质: 不断地重复某种动作。 sum=0 i=1 对于计算机程序而言,循环 必须具备两个条件: i <=100 F T • 在一定的条件下,重复 • 执行一组语句 sum=sum+i i=i+1 2.必须出现不满足条件的 情况,使循环终止

  5. 循环结构 VBA提供了三种循环语句,分别是: (1)For…Next循环语句; (2)While…Wend循环语句; (3)Do…Loop循环语句。 For…Next

  6. For循环语句 For语句格式: For循环变量=初值 To终值[Step步长] [循环体] Next [循环变量] 循环变量赋初值 F 循环变量未 超出终值? T 循环体 循环变量增加步长

  7. 那么怎样使用For循环 求1+2+3+……+100呢? For循环语句 0 n = For i = To Step Next 1 100 1 n =n + i i

  8. 2+4+6+……+200 For循环语句 ? 0 n = For i = To Step Next 200 2 2 n = n + i i

  9. 2×4×6×……×20 For循环语句 ? 1 n= For i = To Step Next 2 2 20 n = n * i i

  10. For循环语句 For循环变量=初值 To终值[Step步长] [循环体] Next [循环变量] • 执行条件: • 若步长>0,初值<终值 • 若步长<0,初值>终值 • 若初值=终值,则不管步长>0还是步长<0, 循环体都执行一次

  11. For循环语句 For循环变量=初值 To终值[Step步长] [循环体] Next [循环变量] • 循环变量:必须为数值型。 • For语句和Next语句必须成对出现。 • Next后的循环变量可以省略。 • 步长为1时,Step 1可以省略。 • 循环次数确定,为

  12. For循环应用 水仙花数是指一个 n 位数 ( n≥3 ),它的每位上的数字的 n 次幂之和等于它本身。(例如153 =13+53+33) 求所有三位数中的水仙花数? 水仙花数 Sub narcissus() Dim i as integer Dim a as integer Dim b as integer Dim c as integer For i = 100 To 999 a = i Mod 10 b = i \ 10 Mod 10 c = i \ 100 If a ^ 3 + b ^ 3 + c ^ 3 = i Then Debug.Print i End If Next i End Sub

  13. For循环应用 Sub jitu() Dim x as integer Dim y1 as Single Dim y2 as Single For x = 0 To 35 y1 = 35 - x y2 = (94 - 2*x) / 4 If y1 = y2 Then Debug.Print x, y1 End If Nextx End Sub 鸡兔同笼 今有鸡兔同笼,上有三十五头,下有九十四足,问鸡兔各几何? Exit For

  14. For循环应用 作 业 要求: 1、设计窗体如图,头和脚的 个数由文本框获取; 2、单击命令按钮时,计算鸡 和兔子的个数,并将结果显示 在文本框中; 3、如果输入的头和脚的个数 不是合理值,则当单击命令按 钮时,弹出消息框,提示信息 为“请重新输入”。

More Related