1、2.3 程序基本结构,顺序结构,选择结构,循环结构,程序基本结构:,一、顺序结构,P33例 Private Sub Command1_Click() Dim a As Single, b As Single, c As Single a = Val(InputBox(“输入a“) b = Val(InputBox(“输入b“) Print “交换前:a=“; a, “b=“; b c = a a = b b = c MsgBox “交换后a=” & a & “b=” & b, vbOKOnly, “交换结果” End Sub, 使用对话框,一、输入框(InputBox) a=Val(input
2、box(“输入a”),二、信息对话框(MsgBox)msgbox “交换后a=“ & a & “b=“ & b ,vbOKOnly ,“交换结果“,二、选择结构,单分支选择结构,例 Private Sub Form_Click() Dim a As Double a = Val(InputBox(“请输入一个数“) If a 0 ThenMsgBox “你输入的是一个负数“ End If Print a End Sub,双分支选择结构,例P35 Private Sub cmdok_Click() Dim v As Single v = Val(InputBox(“输入速度(km/s)“) If
3、 v = 7.91 Thenlblshow.Caption = “飞船成功飞行“ Elselblshow.Caption = “飞船不能升空“ End If End Sub,多分支选择结构,0-59不及格 60-69及格 70-79中等 80-89良好 90-100优秀,Select case 表达式 case 表达式列表1语句组1case 表达式列表2语句组2 case else语句组n End Select,Private Sub Form_Click() Dim x As Single x = Val(InputBox(“请输入成绩“) If x = 90 ThenPrint “优秀“
4、ElseIf x = 80 ThenPrint “良好“ ElseIf x = 70 ThenPrint “中等“ ElseIf x = 60 ThenPrint “及格“ ElsePrint “不及格“ End If End Sub,Private Sub Form_Click() Dim x As Single x = Val(InputBox(“请输入成绩“) Select Case xCase Is = 90Print “优秀“Case Is = 80Print “良好“Case Is = 70Print “中等“Case Is = 60Print “及格“Case ElsePrint
5、 “不及格“ End Select End Sub,三、循环结构,For 循环变量=初始值 To 终止值(Step步长)循环体 Next 循环变量,Private Sub Form_Click() Dim i As Integer For i = 1 To 10Print i Next i End Sub,For Next循环,Private Sub Form_Click() Dim i As Integer, s As Integers = 0 For i = 1 To 100s = s + i Next i Print “1-100之间所有数的和是“; s End Sub,Y,小猴在某天摘
6、桃子若干个,当天吃掉一半多一个;第2天吃了剩下的桃子的一半多一个;以后每天都吃尚存桃子的一半多一个,到第7天要吃时只剩下一个,问小猴共摘了多少个桃子?,分析:这是一个“递推”问题,先从最后一天推出倒数第2天的桃子树,再从倒数第2天的桃子推出倒数第3天的桃子数设第n天的桃子为X n,那么它是前一天的桃子数为X n-1的一半减1,即X n-1=( X n+1)*2,For 循环变量=初始值 To 终止值(Step步长)循环体 Next 循环变量,Do while 条件循环体 Loop,Do while loop循环 和 DoLoop whlie循环,Do 循环体 Loop while 条件,先判断
7、,后执行,先执行,后判断,Private Sub Form_Click() Dim i As Integer For i = 1 To 10Print i Next i End Sub,Private Sub Form_Click() Dim i As Integer i = 1 Do While i = 10Print ii = i + 1 Loop End Sub,Private Sub Form_Click() Dim i As Integer dim s As Integer i = 1 s = 0 Do While i = 100 s = s + i i = i + 1 Loop P
8、rint “和是:“; s End Sub,Private Sub Form_Click() Dim i As Integer dim s As Integers = 0 For i = 1 To 100s = s + i Next i Print “和是“; s End Sub,注:ForNext语句和Do whileLoop语句在许多情况下是可以通用的,但是ForNext语句更适合用于循环次数容易确定的情况,而Do whileLoop语句的使用则更灵活些。,例: 假设我国现有人口12亿,若年增长率为1.5%,试计算多少年后我国人口将增加到20亿。,r为年增长率,i为年数,则i年后人口数为p
9、=p(1+r)i,Private Sub Form_Click() Dim p As Single, r As Single, i As Integer p = 12 r = 0.015 i = 0 Do While p 20p = p * (1 + r)i = i + 1 Loop Print I ; “年后,我国人口将达到” ; p ; “亿“ End Sub,三、循环结构-双重循环,For 循环变量=初始值 To 终止值(Step步长)循环体 Next 循环变量,复习回顾For-next循环结构:,1、编程计算S=1+2+3+100的值 2、编程计算1到100之间所有偶数的和,要求Ste
10、p值为2 3、在程序运行过程中任意输入一个自然数n, 求1到n之间所有偶数的和,例1:,请用for-next循环结构编写如下图所示的图形,结论:如果想让下一个print 语句显示的内容与上一个print语句的内容连在一起,则在上一个Print后面加一个“分号”即可。我们称之为续行符。,提出问题,思考,如何用for-next循环结构实现如图所示的图形,双重循环,循环体内部还有循环语句的循环叫做多重循环,又叫多层循环或嵌套循环。如果内部只有一层循环,叫双重循环。,Private Sub Form_Click() Dim i As Integer, j As Integer For i = 1 To
11、 5For j = 1 To 5Print “*“;Next j Print Next i End Sub,内循环,j 控制每行的*个数,外循环,i控制输出的行数,例2:,例3:,Private Sub Form_Click() Dim i As Integer, j As Integer For i = 1 To 5For j = 1 To 2*i-1Print “*“;Next j Print Next i End Sub,补充知识点:,1、双重循环的执行过程是外循环执行一次,内循环执行一遍,在内循环结束后,再进行下一次外循环,如此反复,直到外循环结束。 2、对于双重循环,各层循环的循环控制变量不能同名。 3、外循环必须完全包含内循环,不能交叉。,总结:,Private Sub Form_Click() For i = 1 To 7Print Tab(10 - i);For j = 1 To 2 * i - 1Print “*“;Next j Next i End Sub,对*输出位置进行定位并控制换行,例4:P39,思考:,