If...Then...Else 语句 - Visual Basic

If...Then...Else 语句 - Visual Basic

根据表达式的值有条件地执行一组语句。

语法

' Multiline syntax:

If condition [ Then ]

[ statements ]

[ ElseIf elseifcondition [ Then ]

[ elseifstatements ] ]

[ Else

[ elsestatements ] ]

End If

' Single-line syntax:

If condition Then [ statements ] [ Else [ elsestatements ] ]

指向示例代码的快速链接

本文包括几个示例,说明了 If...Then...Else 语句的用法:

多行语法示例

嵌套语法示例

单行语法示例

组成部分

condition

必需。 表达式。 计算结果必须为 True 或 False,或者计算结果为可隐式转换为 Boolean 的数据类型。

如果表达式是一个 NullableBoolean 变量,其计算结果为 Nothing,则条件被视为表达式为 False,并且计算 ElseIf 块(如果它们存在),或者执行 Else 块(如果它存在)。

Then

在单行语法中是必需的;在多行语法中是可选的。

statements

可选。 后面If的一个或多个语句 ...如果计算结果为Then,则执行。 conditionTrue

elseifcondition

如果存在 ElseIf,则为必需。 表达式。 计算结果必须为 True 或 False,或者计算结果为可隐式转换为 Boolean 的数据类型。

elseifstatements

可选。 后面ElseIf的一个或多个语句 ...如果计算结果为Then,则执行。 elseifconditionTrue

elsestatements

可选。 如果先前的 condition 或 elseifcondition 表达式没有被计算为 True,则执行一个或多个语句。

End If

终止 If...Then...Else 块的多行版本。

注解

多行语法

当遇到 If...Then...Else 语句时,会测试 condition。 如果 condition 为 True,则执行 Then 后面的语句。 如果 condition 为 False,则按顺序计算每个 ElseIf 语句(如果有)。 找到 Trueelseifcondition 时,将执行紧跟在关联的 ElseIf 之后的语句。 如果没有 elseifcondition 计算为 True,或者如果没有 ElseIf 语句,则执行 Else 后面的语句。 在执行 Then、ElseIf 或 Else 后面的语句后,继续执行 End If 后面的语句。

ElseIf 和 Else 子句都是可选的。 可以在 ElseIf...If...Then 语句中包含任意数量的 Else 子句,但 ElseIf 子句不能出现在 Else 子句之后。

If...Then...Else 语句可以相互嵌套。

在多行语法中,If 语句必须是第一行的唯一语句。

ElseIf、Else 和 End If 语句前面只能有一个行标签。

If...Then...Else 块必须以 End If 语句结尾。

提示

当你计算具有几个可能值的单个表达式时,Select...Case 语句可能更有用。

单行语法

可以将单行语法用于单个条件,并在符合条件时执行代码。 但是,多行语法提供了更多的结构和灵活性,并且更易于读取、维护和调试。

检查 Then 关键字后面的内容以确定语句是否为单行 If。 如果在同一行的 Then 后面出现除注释以外的任何内容,则该语句被视为单行 If 语句。 如果 Then 不存在,则它必须是多行 If...Then...Else 的开头。

在单行语法中,可以执行多个语句作为 If...Then 决策的结果。 所有语句必须位于同一行,并且用冒号分隔。

多行语法示例

下面的示例说明了 If...Then...Else 语句的多行语法的用法。

'Create a Random object to seed our starting value

Dim randomizer As New Random()

'set our variable

Dim count As Integer = randomizer.Next(0, 5)

Dim message As String

'If count is zero, output will be no items

If count = 0 Then

message = "There are no items."

'If count is 1, output will be "There is 1 item.".

ElseIf count = 1 Then

message = "There is 1 item."

'If count is greater than 1, output will be "There are {count} items.", where {count} is replaced by the value of count.

Else

message = $"There are {count} items."

End If

Console.WriteLine(message)

'This example displays output like the following:

' There are 4 items.

嵌套语法示例

下面的示例包含嵌套的 If...Then...Else 语句。

Public Sub Main()

' Run the function as part of the WriteLine output.

Console.WriteLine("Time Check is " & CheckIfTime() & ".")

End Sub

Private Function CheckIfTime() As Boolean

' Determine the current day of week and hour of day.

Dim dayW As DayOfWeek = DateTime.Now.DayOfWeek

Dim hour As Integer = DateTime.Now.Hour

' Return True if Wednesday from 2 to 3:59 P.M.,

' or if Thursday from noon to 12:59 P.M.

If dayW = DayOfWeek.Wednesday Then

If hour = 14 Or hour = 15 Then

Return True

Else

Return False

End If

ElseIf dayW = DayOfWeek.Thursday Then

If hour = 12 Then

Return True

Else

Return False

End If

Else

Return False

End If

End Function

'This example displays output like the following:

'Time Check is False.

单行语法示例

下面的示例说明了单行语法的用法。

Private Sub SingleLine()

'Create a Random object to seed our starting values

Dim randomizer As New Random()

Dim A As Integer = randomizer.Next(10, 20)

Dim B As Integer = randomizer.Next(0, 20)

Dim C As Integer = randomizer.Next(0, 5)

'Let's display the initial values for comparison

Console.WriteLine($"A value before If: {A}")

Console.WriteLine($"B value before If: {B}")

Console.WriteLine($"C value before If: {C}")

' If A > 10, execute the three colon-separated statements in the order

' that they appear

If A > 10 Then A = A + 1 : B = B + A : C = C + B

'If the condition is true, the values will be different

Console.WriteLine($"A value after If: {A}")

Console.WriteLine($"B value after If: {B}")

Console.WriteLine($"C value after If: {C}")

End Sub

'This example displays output like the following:

'A value before If: 11

'B value before If: 6

'C value before If: 3

'A value after If: 12

'B value after If: 18

'C value after If: 21

另请参阅

Choose

Switch

#If...Then...#Else 指令

Select...Case 语句

嵌套的控件结构

决策结构

Visual Basic 中的逻辑运算符和位运算符

If 运算符

相关文章

欧洲笔记本电脑品牌排名(欧洲笔记本销量排行)
28365365体育投注

欧洲笔记本电脑品牌排名(欧洲笔记本销量排行)

📅 07-27 👁️ 915
老式臭豆腐乳做法大全(臭豆腐自然发酵方法)
28365365体育投注

老式臭豆腐乳做法大全(臭豆腐自然发酵方法)

📅 07-06 👁️ 6334
借呗额度大揭秘:最高能借多少?影响因素全解析
bet体育365冻卡么

借呗额度大揭秘:最高能借多少?影响因素全解析

📅 06-30 👁️ 7715
如何评价新东方老师朱伟?
365bet平台

如何评价新东方老师朱伟?

📅 07-01 👁️ 4159
三星Galaxy Tab 4 T530(16G/WLAN版)评测导购
28365365体育投注

三星Galaxy Tab 4 T530(16G/WLAN版)评测导购

📅 08-15 👁️ 6614
如何取消raid硬盘
365bet平台

如何取消raid硬盘

📅 07-08 👁️ 1590