课程表

WebPages 教程

WebPages 参考手册

ASP.net Razor

ASP.net MVC

WebForms 教程

WebForms 参考手册

工具箱
速查手册

Razor VB 逻辑

当前位置:免费教程 » 程序设计 » ASP.net

编程逻辑:执行基于条件的代码。

If 条件

VB 允许您执行基于条件的代码。

如需测试某个条件,您可以使用 if 语句。If 语句会基于您的测试来返回 true 或 false:

  • if 语句启动代码块
  • 条件位于 if 和 then 之间
  • 如果条件为真,则执行 if … then 与 end if 之间的代码

实例

  1. @Code
  2. Dim price=50
  3. End Code
  4. <html>
  5. <body>
  6. @If price>30 Then
  7. @<p>The price is too high.</p>
  8. End If
  9. </body>
  10. </html>

运行实例

Else 条件

if 语句能够包含 else 条件

else 条件定义条件为 false 时执行的代码。

实例

  1. @Code
  2. Dim price=20
  3. End Code
  4. <html>
  5. <body>
  6. @if price>30 then
  7. @<p>The price is too high.</p>
  8. Else
  9. @<p>The price is OK.</p>
  10. End If
  11. </body>
  12. </htmlV>

运行实例

注释:在上面的例子中,如果价格不大于 30,则执行其余的语句。

ElseIf 条件

可通过 else if 条件来测试多个条件:

实例

  1. @Code
  2. Dim price=25
  3. End Code
  4. <html>
  5. <body>
  6. @If price>=30 Then
  7. @<p>The price is high.</p>
  8. ElseIf price>20 And price<30
  9. @<p>The price is OK.</p>
  10. Else
  11. @<p>The price is low.</p>
  12. End If
  13. </body>
  14. </html>

运行实例

在上面的例子中,如果第一个条件为 true,则执行第一个代码块。

否则,如果下一个条件为 true,则执行第二个代码块。

您能够设置任意数量的 else if 条件。

如果 if 和 else if 条件均不为 true,则执行最后一个 else 代码块。

Select 条件

select 代码块可用于测试一系列具体的条件:

实例

  1. @Code
  2. Dim weekday=DateTime.Now.DayOfWeek
  3. Dim day=weekday.ToString()
  4. Dim message=""
  5. End Code
  6. <html>
  7. <body>
  8. @Select Case day
  9. Case "Monday"
  10. message="This is the first weekday."
  11. Case "Thursday"
  12. message="Only one day before weekend."
  13. Case "Friday"
  14. message="Tomorrow is weekend!"
  15. Case Else
  16. message="Today is " & day
  17. End Select
  18. <p>@message</p>
  19. </body>
  20. </html>

运行实例

"Select Case" 之后是测试值 (day)。每个具体的测试条件以 case 关键词开头,其后允许任意数量的代码行。如果测试值匹配 case 值,则执行代码行。

select 代码块可为其余的情况设置默认的 case (default:),允许在所有 case 均不为 true 时执行代码。

转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号