vb循环回顾

前端之家收集整理的这篇文章主要介绍了vb循环回顾前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1.If语句

行:If 条件 then 语句组1 [else 语句组2]

块:if 条件 then

[语句组1]

[else

[语句组2]

]

end if

2.Select case

select case 表达式:

[case 表达式列表1

[语句块1]]

[case 表达式列表2

[语句块2]]

······

[case else

[语句块n]]

End select

Private Sub Form_Click()

Dim intx As Integer

intx = InputBox("请输入x的值:")

Select Case intx

 Case Is < 0

  MsgBox "函数无定义"

  Case Is < 20

   MsgBox 25 * intx - 5

   Case 20 To 50

   MsgBox 50

   Case Is < 100

   MsgBox 5 * intx + 19

   Case Else

    MsgBox 100

    End Select

    

   

End Sub

 


3.for 循环

注意步长与初值和终值的关系

可用exit for 跳出循环

s=0

For i=1 to 100

 s=s+I

   If s>4000 then exit for

Next I


4.while 循环

例子:求1+2+···+100

Private Sub Form_Click()

 i = 0

 s = 0

(do) While i <= 100

  s = s + i

  i = i + 1

 wend( Loop)

  Print s

  

End Sub




5.Do 循环(可以用exit do 跳出循环)

1. Do while

循环体

Loop

注意:do while 循环与while循环完全相同,即首先判断条件,当条件成立时重复执行循环体。(例子如上)

2.do ···loop while

注意:此循环是先执行一次,再判断条件。当条件成立时,重复执行循环。

Do

循环体

Loop while 条件

例子:

Private Sub Form_Click()

Dim i As Integer

Dim s As Integer

 

   i = 0

   s = 0

   

 Do

   s = s + i

   i = i + 1

 Loop While i <= 100

 

 Print s

 

End Sub


3.Do Until

注意:DO until与DO while结构类似,只是前者在条件不满足时成立,后者反之。

例子:

Private Sub Form_Click()

Dim i As Integer

Dim s As Integer

 

Do Until i > 100

  s = s + i

  i = i + 1

Loop

  

  Print s

  

End Sub


4.DO ···loop until

注意:Do ···loop until 结构与Do until 类似,只是前者是先执行一次循环,再判断,在条件不成立时重复执行循环体。

例子:

Private Sub Form_Click()

 Dim i As Integer

 Dim s As Integer

 

  Do Until i > 100

   s = s + i

   i = i + 1

 Loop

 

 Print s

 

End Sub


6.for each 循环

注意:for each 循环与for循环类似,但是for each用于数组或对象集合中的每一个元素重复一组语句,在数组中的运用我们并不陌生。

例子:

Private Sub Form_Click()

 Dim a(100) As Integer

  For i = 1 To 100

   a(i) = i

   s = s + a(i)

  Next i

   Print s

End Sub

猜你在找的VB相关文章