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.