2012年计算机考试二级VB考点及习题(4)
例如:1、if 语句:
Private Sub Form_Click()
Dim x As Integer, y As Integer
Dim z As Single
x = 0.2
y = 1
If (x >= 1) Then
z = Log(x)
ElseIf x >= 0 And x < 1 And y >= 0 Then
z = 2 * x - y
Else
z = Exp(x + y)
End If
Print "z=" + Str(z)
End Sub
结果: z=-1
2、Select Case语句
Private Sub Form_Click()
Dim ch As String, i As Integer
ch = "c"
Select Case ch
Case "A", "a"
i = 1
Case "B", "b"
i = 2
Case "C", "c"
i = 3
Case "D", "d"
i = 4
Case Else
i = 5
End Select
Print "运行结果" + Ucase(ch) + Str(i)
End Sub
结果:运行结果C3
② 循环结构
1、For------Next (循环次数的计算)
特例:
例如:有循环:for I=n1 to n2 step n3 下列哪些语句会影响循环次数( )
A. n1=n1+I
B. n3=2*n3
C. n2=n2+n3
D. I=I+n3
2、Do------Loop (While,Until)
例如:运行下面的程序,单击窗体后在窗体上的内容是( );若将程序中的A语句与B语句的位置互换,再次执行程序,单击窗体后在窗体上显示的内容是( )
Private Sub Form_Click()
Dim a As Integer, b As Integer
a = 0: b = 0
Do While a <= 8
a = a + 2 ’A语句
b = b + a * a ’B语句
Loop
Print a, b
End Sub
例如:窗体上显示的结果:
Private Sub Form_Click()
Dim p As Integer, i As Integer, n As Integer
p = 2: n = 20
For i = 1 To n Step p
p = p + 2
n = n - 3
i = i + 1
If p >= 10 Then Exit For
Next i
Print i, p, n
End Sub
一、 InputBox函数
MsgBox函数 (注意参数含义及顺序)
x=InputBox(_____,______,_______)
写出显示下图信息框的MsgBox函数