它是控制語句最簡單的形式,經常用於決策和改變程式執行的控制流程。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之後)。
流程圖

示例
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決策結構
下一篇:
VB.Net迴圈
