第 2 章 visual basic 6.0 编程基础 (第二部分)
DESCRIPTION
2.10. 2.11. 2.12. 2.7. 2.8. 2.9. 基本流程结构. 数组. 过程. Visual Basic 6.0 程序组成. 综合案例. 实训. 第 2 章 Visual Basic 6.0 编程基础 (第二部分). 2.7 基本流程结构. 控制结构可控制程序执行的流程。如果未使用控制流语句,程序便从左至右、自顶向下地贯穿这些语句。有些简单程序可以只用单向流程来编写,有些流程可以依靠运算符的优先级来控制,但任何编程语言的效力和用途皆由其通过结构和循环改变语句顺序的能力而得。. 2.7.1 顺序结构. - PowerPoint PPT PresentationTRANSCRIPT
-
2 Visual Basic 6.0
-
2.7
-
2.7.1
-
2-6
-
Private Sub Command1_Click() Dim x, y As Double Const a = 3 Const b = -5 x = Val(Text1.Text) y = a * x ^ 2 + b * x + 1 y = y / Abs(a + b) Text2.Text = Str$(y)End Sub
-
2.7.2 Visual Basic 6.0 IfThen IfThenElse Iif Select Case
-
1IfThenIf Then If Then []End IfVisual Basic 6.0 TrueFalseFalseTrueTrueVisual Basic 6.0Then
-
2 IfThenElseIf 1 Then [1][Else If 2 Then [2]] ...[Else [n]]End IfVisual Basic 6.01FalseVisual Basic 6.02TrueTrueVisual Basic 6.0End IfElsen-1FalseVisual Basic 6.0Else
-
3IIfIIf()
-
4Visual Basic 6.0Select CaseIfThenElseSelect CaseIfThenElseSelect CaseSelect CaseVisual Basic 6.0CaseCaseSelect Case test [Case 1 [1]] [Case 2 [2]] ... [Case Else [n]]End Select
-
2-7
-
Private Sub Command1_Click() Dim a As Integer' a = Val(Text1.Text)'a If a < 0 Then'a a = -a End If Text2.Text = Str$(a)End Sub
Private Sub Command2_Click() Text1.Text = "" Text2.Text = ""End Sub
-
2-8
-
Private Sub Command1_Click() Dim x As Integer x = Val(Text1.Text) If (x Mod 100) Then'X100 If (x Mod 4 = 0) Then'x4100 Text2.Text = "yes" Else'x4100 Text2.Text = "no" End If ElseIf (x Mod 400 = 0) Then'x100400 Text2.Text = "yes" Else Text2.Text = "no" End IfEnd Sub
-
Private Sub Command2_Click() Text1.Text = "" Text2.Text = ""End Sub
-
2-9
-
Case 6 Text2.Text = "" Case 7 Text2.Text = "" Case 8 Text2.Text = "" Case 9 Text2.Text = "" Case 10 Text2.Text = "" Case 11 Text2.Text = "" Case 0 Text2.Text = "" End SelectEnd SubPrivate Sub Command1_Click() Dim year As Integer, a As Integer year = Val(Text1.Text) a = year Mod 12 Select Case a Case 1 Text2.Text = "" Case 2 Text2.Text = "" Case 3 Text2.Text = "" Case 4 Text2.Text = "" Case 5 Text2.Text = ""
-
ElseIf a=6 Then Text2.Text = "" ElseIf a=7 Then Text2.Text = "" ElseIf a=8 Then Text2.Text = "" ElseIf a=9 Then Text2.Text = "" ElseIf a=10 Then Text2.Text = "" ElseIf a=11 Then Text2.Text = "" Else Text2.Text = "" End IfEnd SubIfThenElsePrivate Sub Command1_Click() Dim year As Integer, a As Integer year = Val(Text1.Text) a = year Mod 12 If a=1 Then Text2.Text = "" ElseIf a=2 Then Text2.Text = "" ElseIf a=3 Then Text2.Text = "" ElseIf a=4 Then Text2.Text = "" ElseIf a=5 Then Text2.Text = ""
-
2.7.3 DoLoop ForNext
-
1DoLoop DoDoLoopIfThen TrueFalseDoLoopTrueDo While () []Loop Visual Basic 6.0DoFalseTrueVisual Basic 6.0Do WhileTrueFalse
-
DoLoopDo []Loop While () FalseTrue
-
2ForNext DoForForFor = To [Step ] []Next [] Step1= +
-
For Visual Basic 6.0 Next []= +
-
2-10 121%20
-
Private Sub Form_Click() Dim a As Double Dim r As Single Dim i As Integer a = 12 r = 0.01 i = 0 Do While a < 20 '20 a = a * (1 + r) i = i + 1 Loop Print i ;"20"End SubDoLoop WhilePrivate Sub Form_Click() Dim a As Double Dim r As Single Dim i As Integer a = 12 r = 0.01 i = 0 Do a = a * (1 + r) i = i + 1 Loop While a < 20 '20 Print i "20"End Sub
-
2-11 m=1+2+3+4+100
-
Private Sub Form_Click() Dim m As Integeri As Integer m = 0 For i = 1 To 100'1 m = m + i Next i Print mEnd Sub
-
2.7.4
-
1 ForNextIfThennestVisual Basic 6.0
-
2 ExitForDoFor = To [Step ] [] [Exit For] []Next [[, ] [,]]Do [{While | Until} ] [] [Exit Do] [] Loop Exit DoDoExit ForExit Do
-
2-12 121%20
-
Private Sub Form_Click() Dim a As Double Dim r As Single Dim i As Integer a = 12 r = 0.01 For i=0 to 1000 a = a * (1 + r) If a >= 20 Then Exit For '20 Next i Print i; "20"End Sub
-
2.8 Visual Basic 6.0S(i) SiS(8)S
-
2.8.1 3 Public Private Private
-
Long-2 147 483 648~2 147 483 647Dim A(4) As Integer 'A(0)A(1)A(2)A(3)A(4)5Dim S (20) As Double 'S(0)S(20)21PublicDimPublic A(4) As IntegerPublic S (20) As Double 50421020
-
ToLongDim A (1 To 5) As IntegerDim S (100 To 120) As StringA1~5S100~120
-
LboundUboundLbound() 'Ubound() '
-
2.8.2 Visual Basic 6.0
-
1PublicDimStaticDimDim A ()
-
2ReDimReDim A (X + 1)ReDimDimStaticReDimReDimReDim
-
2.8.3 Visual Basic 6.0For EachNext
-
1Number(1)A(2)Num(0)B(4)
-
213For i=1 to 5 A(i)=3Next i
-
2Array()ArrayStatic Number Number=Array(1,2,3,4,5) Array()Varient 0Option BaseArray()UBound() Array()
-
41Dim b(4),a(6)b(2)=a(3)2For
-
3Dim a(4) as integer,b() as integerA(0)=0:a(1)=5:a(2)=10:a(3)=13:a(4)=14B=a
-
5For EachNextFor EachNextForNextForNextFor EachNextFor Each In []Next []
-
For EachNext
-
2-13
-
Private Sub Form_Click() Dim a(), b() ' a = Array(1, 2, 3, 4, 5) ' ReDim b(UBound(a)) ' For i = 0 To UBound(a) ' b(i) = a(i) Next i For i = 0 To UBound(a) ' Print b(i); Next iEnd Sub
-
2-14 A
-
Private Sub Form_Click() Dim sum As Integer Dim A(1 To 10) As Integer ' For i = 1 To 10 ' A(i) = 1 Next i sum = 0 For Each x In A 'For EachNext sum = sum + x Next x Print sum 'End Sub
-
2-15 Option Base 1Private Sub Form_Click() Dim abc(10) As Integer ' For i = 1 To 10 '0~100 abc(i) = Int(Rnd * 100) Next i Print "" For i = 1 To 10 ' Print abc(i); Next i Print Print "For EachNext" For Each x In abc 'For EachNext Print x; NextEnd Sub
-
2.9 12
-
2.9.1 Visual Basic
-
1Visual Basic 6.0SubFunction
-
2
-
Private Sub _ ( ) End SubPrivate Sub Form_ () End Sub
-
2.9.2 Sub1[Private | Public][Static] Sub [] [Exit Sub] End Sub
2Sub
-
2-16 Sub
-
/Dim area As SingleSubSubSub recarea(rlen, rwid) area = rlen * rwidEnd Sub
-
Private Sub Command1_Click() Dim a As Single, b As Single a = Val(Text1.Text) b = Val(Text2.Text) recarea a, b 'Call recarea(a, b) Text3.Text = areaEnd SubPrivate Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = ""End Sub
-
2-17 TriangleForm_Click
-
Private Sub triangle(Str As String, n As Integer) Dim i As Integer, j As Integer For i = l To n Print Tab(16 - i); For j = 1 To 2 * i - 1 Print Str; Next j Print Next iEnd Sub
-
Private Sub Form_Click() Dim char As String * 1, n As Integer char = "*" n = 5 Call triangle(char, n) char = "+" triangle char, 3End Sub
-
2.9.3 FunctionVisual Basic 6.0SqrCosChrFunctionFunctionSubFunctionFunction
-
1Function[Static] [Public | Private] Function [][As]
=[Exit Function]End Function As Sub
-
2FunctionSubFunctionSubFunction
-
3Function Visual Basic 6.0CosTxtPrint 10 * TxtX = TxtIf Txt = 10 Then Print "Out of Range";" X = ";10 * TxtSubCall Sin(x)Sin x
-
2-18 Function
-
FunctionFunction recarea(rlen, rwid) As Single recarea = rlen * rwidEnd Function
-
Private Sub Command1_Click() Dim a As Single, b As Single a = Val(Text1.Text) b = Val(Text2.Text) Text3.Text = Str(recarea(a, b))End Sub
Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Text3.Text = ""End Sub
-
2.9.4 SubFunctionVisual Basic 6.0(ByRef)(ByVal)
-
1ByVal
-
2Visual Basic 6.0ByVal
-
3
-
2-19 Private Sub Increase(x As Integer) x = x + 1End Sub
Private Sub Decrease(ByVal x As Integer) x = x - 1End Sub
Private Sub Form_Click() Dim i As Integer, j As Integer i = 1000 Increase i Print i j = 1000 Decrease j Print jEnd Sub
-
2.10 Visual Basic 6.0Visual Basic 6.0Visual Basic 6.0
-
2.10.1 ModuleVisual Basic 6.03
-
2.10.2 Visual Basic 6.0
-
2.11
- Public Function Sushu(ByVal x As Integer) Dim k, i As Integer k = Int(Sqr(x)) i = 2 Sushu = 0 Do While i
-
Dim a(0 To 20) As IntegerDim j, m, n As IntegerPrivate Sub Command1_Click() Command2.Enabled = True FontSize = 24 ' Print ' Print "20" For j = 1 To 20 a(j) = CInt(Rnd * 100) '100 Print a(j); ' If j Mod 5 = 0 Then '5 Print End If Next jEnd Sub
-
Private Sub Command2_Click() n = 0 Print "" For j = 1 To 20 m = Sushu(a(j)) 'a(j) If m = 0 Then ' Print a(j); n = n + 1 End If If n Mod 5 = 0 And n 0 Then '5 n = 0 Print End If Next jEnd Sub
-
2.12