VB.Net 流程控制

  • 决策

    决策结构要求程序员指定一个或多个要由程序评估或测试的条件,以及确定该条件为真的情况下要执行的一条或多条语句,以及指定该条件时要执行的其他语句(可选)确定为假。下面显示的是大多数编程语言中常见的典型决策结构的一般形式-
    c decision_making
    VB.Net 提供以下类型的决策语句。
    声明 描述
    If ... Then 声明 一个 If ... Then 语句包含一个布尔表达式后跟一个或多个语句。
    If...Then...Else 声明 一个If...Then... 语句可以跟着一个可选的Else语句,当布尔表达式是假的,其执行。
    嵌套If 语句 您可以在另一个If或Else if语句中使用一个If或Else if语句。
    Select Case 语句 Select Case 语句允许一个变量来针对值的列表平等进行测试。
    嵌套的Select Case语句 您可以在另一个Select Case语句中使用 Select Case 语句。
    范例 - If...Then 声明
    condition1
    
    Module decisions
       Sub Main()
          'local variable definition 
          Dim a As Integer = 10
    
          ' check the boolean condition using if statement 
          If (a < 20) Then
             ' if condition is true then print the following 
             Console.WriteLine("a is less than 20")
          End If
          Console.WriteLine("value of a is : {0}", a)
          Console.ReadLine()
        End Sub
    End Module
    
    尝试一下
    范例 - if...else声明
    condition1
    
    Module decisions
       Sub Main()
           'local variable definition '
          Dim a As Integer = 100
    
          ' check the boolean condition using if statement 
          If (a < 20) Then
              ' if condition is true then print the following 
              Console.WriteLine("a is less than 20")
          Else
              ' if condition is false then print the following 
              Console.WriteLine("a is not less than 20")
          End If
          Console.WriteLine("value of a is : {0}", a)
          Console.ReadLine()
       End Sub
    End Module
    
    尝试一下
    If...Else If...Else
    
    Module decisions
       Sub Main()
          'local variable definition '
          Dim a As Integer = 100
          ' check the boolean condition '
          If (a = 10) Then
              ' if condition is true then print the following '
              Console.WriteLine("Value of a is 10") '
          ElseIf (a = 20) Then
              'if else if condition is true '
              Console.WriteLine("Value of a is 20") '
          ElseIf (a = 30) Then
              'if else if condition is true  
              Console.WriteLine("Value of a is 30")
          Else
              'if none of the conditions is true 
              Console.WriteLine("None of the values is matching")
          End If
          Console.WriteLine("Exact value of a is: {0}", a)
          Console.ReadLine()
       End Sub
    End Module
    
    尝试一下
    范例 - 嵌套if语句
    
    Module decisions
       Sub Main()
          'local variable definition
          Dim a As Integer = 100
          Dim b As Integer = 200
          ' check the boolean condition 
          
          If (a = 100) Then
             ' if condition is true then check the following 
             If (b = 200) Then
                ' if condition is true then print the following 
                Console.WriteLine("Value of a is 100 and b is 200")
             End If
          End If
          Console.WriteLine("Exact value of a is : {0}", a)
          Console.WriteLine("Exact value of b is : {0}", b)
          Console.ReadLine()
       End Sub
    End Module
    
    尝试一下
    范例 - Select Case 语句
    condition1
    
    Module decisions
       Sub Main()
          'local variable definition
          Dim grade As Char
          grade = "B"
          Select grade
              Case "A"
                  Console.WriteLine("Excellent!")
              Case "B", "C"
                  Console.WriteLine("Well done")
              Case "D"
                  Console.WriteLine("You passed")
              Case "F"
                  Console.WriteLine("Better try again")
              Case Else
                  Console.WriteLine("Invalid grade")
          End Select
          Console.WriteLine("Your grade is  {0}", grade)
          Console.ReadLine()
       End Sub
    End Module
    
    尝试一下
    范例 - 嵌套的Select Case语句
    
    Module decisions
       Sub Main()
          'local variable definition
          Dim a As Integer = 100
          Dim b As Integer = 200
          Select a
             Case 100
                Console.WriteLine("This is part of outer case ")
                Select Case b
                   Case 200
                      Console.WriteLine("This is part of inner case ")
                End Select
             End Select
          Console.WriteLine("Exact value of a is : {0}", a)
          Console.WriteLine("Exact value of b is : {0}", b)
          Console.ReadLine()
       End Sub
    End Module
    
    尝试一下