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

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

流程图

VB.Net if statement

示例

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

编译并执行上述代码后,将产生以下结果:

a is less than 20
value of a is : 10
vb.net_decision_making.htm
广告
© . All rights reserved.