1.If
Private Sub Command1_Click()
Dim a As String "声明a是字符串形式的变量"
a = Text1.Text
If a = "你好" Then
MsgBox "你输入的是你好"
ElseIf a <> "你好" Then 或 Else
MsgBox "你输入的并非是你好"
End If
End Sub
2.if
Private Sub Command1_Click()
Dim a As String
a = Text1.Text
If a = "邱吉尔" Then
MsgBox "恭喜你,回答正确"
ElseIf a = "罗斯福" Then
MsgBox "恭喜你,回答正确"
ElseIf a = "斯大林" Then
MsgBox "恭喜你,回答正确"
Else
MsgBox "对不起,你真笨"
End If
End Sub
3.
Private Sub Command1_Click()
Dim a, b As Double
a = Rnd
b = Rnd
If a > b Then
MsgBox "本轮小张胜"
ElseIf a < b Then
MsgBox "本轮小王胜"
Else
MsgBox "本轮作费"
End If
End Sub
4.Select case
Private Sub Command1_Click()
Dim a As String
a = Text1.Text
Select Case a
Case "红色"
Label1.BackColor = RGB(255, 0, 0)
Case "绿色"
Label1.BackColor = RGB(0, 255, 0)
Case "蓝色"
Label1.BackColor = RGB(0, 0, 255)
Case Else
Label1.BackColor = RGB(255, 255, 0)
End Select
End Sub
5.do .....loop
Private Sub Command1_Click()
Dim a, b, s, i As Integer "整型"
a = Val(text1.Text)
b = Val(text2.Text)
s = 0
i = a
Do While i <= b
s = s + i
i = i + 1
Loop
MsgBox "计算结果为" & s
Do loop 的另一种形式
Do
s=s+i
i=i+1
Loop while i<=b
End Sub
"计算两个整数之间所有整数之和(包括这两个整数)"
6. do while 当条件满足时候执行
do until 当条件不满足时候执行
Do
s=s+i
i=i+1
Loop until i>b
7.While...(条件满足时) Wend(执行)
Private Sub Command1_Click()
Dim a, b, s, i As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
s = 0
i = a
While i <= b
s = s + i
i = i + 1
Wend
MsgBox "计算结果为:" & s
End Sub
8.For a to b Step 2 计算a到b之间间隔为2的所有数之和
Private Sub Command1_Click()
Dim a, b, s, i As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
s = 0
i = a
For i = a To b Step 2
s = s + i
Next
MsgBox "计算结果为:" & s
End Sub