VB.Net - If...Then 语句

它是最简单的控制语句形式,经常用于决策和改变程序执行的控制流程。 if-then 语句的语法是 −

If condition Then 
[Statement(s)]
End If

其中,condition 是布尔或关系条件,Statement(s) 是简单或复合语句。 If-Then 语句的示例是 −

If (a <= 20) Then
   c= c+1
End If

如果条件计算结果为 true,则将执行 If 语句内的代码块。 如果条件计算结果为 false,则将执行 If 语句结束后(结束 End If 之后)的第一组代码。

流程图

VB.Net if 语句

示例

Module decisions
   Sub Main()
      '局部变量定义
      Dim a As Integer = 10

      ' 使用 if 语句检查布尔条件
      If (a < 20) Then
         ' 如果条件为 true 则打印以下内容
         Console.WriteLine("a is less than 20")
      End If
      Console.WriteLine("value of a is : {0}", a)
      Console.ReadLine()
    End Sub
End Module

当上面的代码被编译并执行时,会产生以下结果 −

a is less than 20
value of a is : 10

❮ vb.net_decision_making.html