1、2.4 循环的嵌套,循环结构格式:,For循环变量=初值 to 终值 step 步长 循环体 Next 循环变量,*,任务1. 输出如下图的五颗星号呢?,Print 换行,print “*”;,For b =1 to 5,Next b,任务2:输出3行图形(五颗星号)呢?,For a =1 to 3Next a,*,*,*,Private sub command1_click() Dim a as integer,b as integer For a =1 to 3For b =1 to 5print “*”;Next b Print 换行 Next a End sub,执行过程,试一试:,实
2、践:5行6 列图形,要求:用For循环嵌套语句,For a =1 To 10For b=1 To 20 Next a Next b,For i =1 To 10For i=1 To 20 Next i Next i,外循环必须完全包含内循环,不能交叉; 内循环变量与外循环变量不能同名;,注意事项:,1.一个循环体中又包含一个完整的循环结构,称为循环的嵌套。,总结:,2.嵌套的每一层循环在逻辑上都是完整的,避免嵌套交叉使用。,3.内循环变量与外循环变量不能同名.,作业,Private Sub Command1_Click()Dim fish1M, fish2M, fish3M As Single
3、Dim num As IntegerDim money As SingleDim fish1, fish2, fish3 As Singlefish1M = CSng(txtFish1.Text) 红狮头的价格fish2M = CSng(txtFish2.Text) 黑骑士的价格fish3M = CSng(txtFish3.Text) 白金刚的价格listMoney.ClearlistMoney.AddItem (“红狮头“ & vbTab & “黑骑士“ & vbTab & “白金刚“)For fish1 = 1 To 25 - 2 红狮头的条数For fish2 = 1 To 25 - fish1 - 1 黑骑士的条数fish3 = 25 - fish1 + fish2 白金刚的条数If fish1 * fish1M + fish2 * fish2M + fish3 * fish3M =30 ThenlistMoney.AddItem (fish1 & vbTab & fish2 & vbTab & fish3)End IfNext fish1Next fish2 End Sub,用For循环嵌套语句,输出以下数字排列图形。,1 1 2 2 3 1 2 3 4 1 2 3 4 5,