Previous topicNext topic
Help > 开发指南 > 编程基础 > VB编程基础 > 决策 >
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语句中的代码块将被执行。 如果条件计算结果为假,则第一组代码在If语句结束后(在结束If之后)将被执行。


流程图:


示例:

Dim a As Integer = 10
' 使用if语句检查布尔条件
If (a < 20) Then
    ' 如果条件为true,则显示以下内容
    Proj.MsgDebug.Add("a is less than 20")
End If
Proj.MsgDebug.Add("value of a is : {0}", a)

'返回结果:a Is less than 20
'返回结果:value Of a Is : 10