编程逻辑:根据条件执行代码。
VB 允许您根据条件执行代码。
要测试条件,您可以使用if 语句。 if 语句根据您的测试返回 true 或 false:
@Code
Dim price=50
End Code
<html>
<body>
@If price>30 Then
@<p>The price is too high.</p>
End If
</body>
</html>
运行示例 »
if 语句可以包含否则条件。
else 条件定义了条件为假时要执行的代码。
@Code
Dim price=20
End Code
<html>
<body>
@if price>30 then
@<p>The price is too high.</p>
Else
@<p>The price is OK.</p>
End If
</body>
</html>
运行示例 »
笔记:在上面的例子中,如果第一个条件为真,就会执行。 else 条件涵盖"everything else"。
可以使用一个测试多个条件否则如果条件:
@Code
Dim price=25
End Code
<html>
<body>
@If price>=30 Then
@<p>The price is high.</p>
ElseIf price>20 And price<30 then
@<p>The price is OK.</p>
Else
@<p>The price is low.</p>
End If
</body>
</html>
运行示例 »
在上面的例子中,如果第一个条件为真,就会执行。
如果不是,那么如果下一个条件为真,则执行该条件。
您可以有任意数量的 else if 条件。
如果 if 或 else if 条件均不成立,则最后一个 else 块(无条件)将覆盖 "everything else"。
选择块可用于测试许多单独的条件:
@Code
Dim weekday=DateTime.Now.DayOfWeek
Dim day=weekday.ToString()
Dim message=""
End Code
<html>
<body>
@Select Case day
Case "Monday"
message="This is the first weekday."
Case "Thursday"
message="Only one day before weekend."
Case "Friday"
message="Tomorrow is weekend!"
Case Else
message="Today is " & day
End Select
<p>
@message</p>
</body>
</html>
运行示例 »
"Select Case" 后面是测试值(天)。每个单独的测试条件都有一个案例值和任意数量的代码行。如果测试值与 case 值匹配,则执行代码行。
选择块可以有 "everything else" 的默认情况(Case Else),如果其他情况都不为真,则运行该默认情况。
截取页面反馈部分,让我们更快修复内容!也可以直接跳过填写反馈内容!