1 / 25

第 5 章 循环结构程序设计 主讲教师:夏其表

第 5 章 循环结构程序设计 主讲教师:夏其表. do…loop 循环结构. for…next 循环结构. while…wend 循环结构. 多重循环结构. 主要内容:. 上节课内容回顾. Do …Loop 循环 For… Next 循环. 问题 :. 计算下式的 和 ,变量 x 与 n 的数值用输入 对话框输入 。. 如何分别用 For 循环和 Do 循环解决?. 这次课内容. 1. While…wend 循环结构 2. 多重循环 ( 循环嵌套 ). 5.4 多重循环 ( 循环嵌套 ).

natala
Download Presentation

第 5 章 循环结构程序设计 主讲教师:夏其表

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. 第5章 循环结构程序设计 主讲教师:夏其表

  2. do…loop循环结构 for…next 循环结构 while…wend 循环结构 多重循环结构 主要内容:

  3. 上节课内容回顾 • Do …Loop循环 • For… Next循环

  4. 问题: 计算下式的和,变量x与n的数值用输入对话框输入。 如何分别用For循环和Do循环解决?

  5. 这次课内容 • 1. While…wend循环结构 • 2. 多重循环(循环嵌套)

  6. 5.4 多重循环(循环嵌套) • 如果在一个循环内完整地包含另一个循环结构,则称为多重循环,或循环嵌套,嵌套的层数可以根据需要而定,嵌套一层称为二重循环,嵌套二层称为三重循环。

  7. 多重循环的常见形式1 For i=…… …… For j=…… …… Next j …… Next i For i=…… …… Do while … …… Loop …… Next i

  8. 多重循环的常见形式2 Do while …… …… For j=…… …… Next j …… Loop • Do while…… …… Do while … …… Loop …… Loop

  9. 例1:s=1!+2!+……+10! 算法: For i=1 to 10 t= i! s = s + t next i s=0 For i=1 to 10 s=s+t Next i t=1 For k=1 to i t=t*k Next k 求i! t=1 For k=1 to i t=t*k Next k

  10. 例2 For i=1 To 7 打印某一行 Next i For i=1 to 7 print space(7-i); for j=1 to 2*i-1 print "*"; next j print Next i

  11. 例3 For i=1 To 7 打印某一行 Next i i 1 2 3 4 5 6 7 i 打印 13 11 9 7 5 3 1 15-2i *个数 空格数 0 1 2 3 4 5 6 i-1

  12. 思 路: For i=1 To 7 Print space(7-i); For j=1 To 2*i-1 Print “*”; Next j Print Next i 不同: 1)例1打印的是字符型数据 例2打印的是数字型数据 2)例1打印的每一行都是“*” 例2打印的每一行都是 不一样的数字 例4

  13. 思 路: For i=1 To 7 Print space(7-i); For j=1 To 2*i-1 8-i; Print “*”; Next j Print 行:7 Next i i 1 2 3 4 5 6 7 i 打印 数字 7 1 8-i 6 5 4 3 2

  14. 思 路: For i=1 To 7 Print space(7-i); For j=1 To 2*i-1 Print trim (8-i); Next j Print Next i 行:7 i 1 2 3 4 5 6 7 i 打印 数字 7 1 8-i 6 5 4 3 2

  15. 例5 Dim i As Integer,j As Integer Dim start As String,count As Integer For i = 1 To 9 If i <= 5 Then start = Space(21 – i) count = 2 * i – 1 Else Space (11+i) start = 19 - 2 * i count = End If Print start ; For j = 1 To count Form1.Print “#” ; Next j I 5 6 7 8 9 I Start 16 17 18 19 20 11+I Count 7 5 3 1 19-2i Print Next i

  16. 对于此类图形,需要注意以下两点: • 1)第一行第一个字符的定位(即确定它的输出位置); • 2)每行之间所输出内容的列数与循环控制变量的对应关系。

  17. 打印如下图形 1 222 33333 4444444 55555555 6666666 77777 888 9

  18. 例6:输出100-200之间所有的素数 • 算法: • For n=100 to 200 • if n是素数 then • print n • end if • Next n For n=100 to 200 For i = 2 To n - 1 If n Mod i = 0 Then Exit For Next i If i > n - 1 Then Print n ; End If Next n n = Val(inputbox(“input n”)) For i = 2 To n - 1 If n Mod i = 0 Then Exit For Next i If i <= n - 1 Then Print n & "is not a prime" Else Print n & "is a prime" End If

  19. 小结 • 各种形式的循环语句都能够互相嵌套,Visual Basic没有具体规定嵌套层数。嵌套的原则是,外层循环与内层循环必须层层相套,循环体之间不能交叉。例如: • 正确的嵌套可以是: • 1)Do While e1 • For a =k1 To k2 • …… • Next a • Loop

  20. 2)For a = kl To k2 • Do While b • …… • Loop • Next a • 等;

  21. 下面是错误的嵌套; • 1)Do While b1 • For a = k1 To k2 • …… • Loop • Next a • 2)While b1 • Do • ……. • Wend • Loop Until b2

  22. 5.3 While…wend循环结构 • 语句格式: While 条件 [语句块] wend • 计算s=1+2+3+…+100 s=0:i=1 while i<=100 s=s+i i=i+1 wend

  23. 例1 将一个正整数逆序输出 确定:循环体和循环条件 • 12345 5 4 3 2 1 • 12345 mod 10 = 5 12345\ 10 =1234 • 1234 mod 10 = 4 1234 \ 10 =123 • 123 mod 10 = 3 123 \ 10 =12 • 12 mod 10 = 2 12 \ 10 =1 • 1 mod 10=1 1 \ 10=0 结束 x = Val(InputBox("input x")) While x <> 0 d = x Mod 10 Print d; x = x \ 10 Wend 循环体 x mod 10 x=x\10 循环条件 x<>0

  24. 作业 1.作业集第五章选择题 2.作业集第五章程序阅读的第1、2、4、5题;程序填空的第1、2、5题;程序设计的第1、5题。

  25. Thank you!

More Related