VB三种基本结构

前端之家收集整理的这篇文章主要介绍了VB三种基本结构前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

VB中最经典的东西是什么?没错就是三种基本结构,顺序结构选择结构、、循环结构。

1、顺序结构

程序代码按照重上到下的顺序一行一行的执行,执行过程找那个没有分支,没有重复。


'输入a b
'交换a b 的值
Dim a As Integer
Dim b As Integer
a = Val(InputBox("a"))
b = Val(InputBox("b"))
c = a
a = b
b = c
MsgBox ("交换后a=" & a & "b=" & b)

2、选择结构

根据不同的条件判断来决定程序执行走向。

a)单分支结构

'当a大于1时  显示a
If a > 1 Then
   Print a
End If

b)双分支结构

'比较X,Y两个值中较大的一个

If x > Y Then
    Print x
Else
    Print Y
End If

c)多分支结构

'按成绩等级分类
If x >= 90 Then
        Print "优秀"
    ElseIf x >= 80 Then
            Print "良好"
        ElseIf x >= 70 Then
                Print ""
            ElseIf x >= 60 Then
                    Print ""
                Else
                    Print ""
End If

d)情况语句


3、循环结构

根据条件判断某些语句可能需要执行0次1次或多次。

1)当型循环


 '循环次数
 Dim I As Long,S As Long
 For I = 1 To 100
   S = S + I
 Next
 MsgBox "For...Next:1+2+3+...+100 = " & S

Dim I As Long,S As Long
   I = 0: S = 0
   Do While I < 100
      I = I + 1
      S = S + I
   Loop
   MsgBox "Do While:1+2+3+...+100 = " & S

Dim I As Long,S As Long
   I = 0: S = 0
   Do Until I > 100
      S = S + I
      I = I + 1
   Loop
   MsgBox "Do Until:1+2+3+...+100 = " & S

Dim I As Long,S As Long
   I = 0: S = 0
   While I < 100
     I = I + 1
     S = S + I
   Wend
   MsgBox "While...Wend:1+2+3+...+100 = " & S


2)直到型循环

Dim I As Long,S As Long
   I = 0: S = 0
   Do
      I = I + 1
      If I > 100 Then Exit Do
      S = S + I
   Loop
   MsgBox "Do...Loop:1+2+3+...+100 = " & S

Dim I As Long,S As Long
 I = 0: S = 0
   Do
      I = I + 1
      S = S + I
   Loop While I < 100
   MsgBox "Loop While:1+2+3+...+100 = " & S

Dim I As Long,S As Long
   I = 0: S = 0
   Do
      S = S + I
      I = I + 1
   Loop Until I > 100
   MsgBox "Loop Until:1+2+3+...+100 = " & S
原文链接:https://www.f2er.com/vb/256405.html

猜你在找的VB相关文章