课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - If...Then...Else语句

当前位置:免费教程 » 程序设计 » VB.Net

If语句后面可以是一个可选的Else语句,当布尔表达式为false时执行。


语法:

VB.Net中If ... Then ... Else语句的语法如下:
  1. If(boolean_expression)Then
  2. 'statement(s) will execute if the Boolean expression is true
  3. Else
  4. 'statement(s) will execute if the Boolean expression is false
  5. End If
如果布尔表达式的计算结果为true,则将执行if代码块,否则将执行代码块。

流程图:


流程图

示例:

  1. Module decisions
  2. Sub Main()
  3. 'local variable definition '
  4. Dim a As Integer = 100
  5. ' check the boolean condition using if statement
  6. If (a < 20) Then
  7. ' if condition is true then print the following
  8. Console.WriteLine("a is less than 20")
  9. Else
  10. ' if condition is false then print the following
  11. Console.WriteLine("a is not less than 20")
  12. End If
  13. Console.WriteLine("value of a is : {0}", a)
  14. Console.ReadLine()
  15. End Sub
  16. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. a is not less than 20
  2. value of a is : 100

The If...Else If...Else语句

If语句后面可以有一个可选的Else if ... Else语句,这对于使用单个If ... Else If语句测试各种条件非常有用。

当使用If ... Else If ... Else语句时,有几点要记住。

1、If可以有零个或一个Else,并且它必须在Else If之后。

2、一个If可以有零到许多Else If的,他们必须在Else之前。

3、一旦Else如果成功,则剩余的Else If或Else的任何一个将被测试。


语法:

VB.Net中if ... else if ... else语句的语法如下:
  1. If(boolean_expression 1)Then
  2. ' Executes when the boolean expression 1 is true
  3. ElseIf( boolean_expression 2)Then
  4. ' Executes when the boolean expression 2 is true
  5. ElseIf( boolean_expression 3)Then
  6. ' Executes when the boolean expression 3 is true
  7. Else
  8. ' executes when the none of the above condition is true
  9. End If

示例:

  1. Module decisions
  2. Sub Main()
  3. 'local variable definition '
  4. Dim a As Integer = 100
  5. ' check the boolean condition '
  6. If (a = 10) Then
  7. ' if condition is true then print the following '
  8. Console.WriteLine("Value of a is 10") '
  9. ElseIf (a = 20) Then
  10. 'if else if condition is true '
  11. Console.WriteLine("Value of a is 20") '
  12. ElseIf (a = 30) Then
  13. 'if else if condition is true
  14. Console.WriteLine("Value of a is 30")
  15. Else
  16. 'if none of the conditions is true
  17. Console.WriteLine("None of the values is matching")
  18. End If
  19. Console.WriteLine("Exact value of a is: {0}", a)
  20. Console.ReadLine()
  21. End Sub
  22. End Module

当上述代码被编译和执行时,它产生以下结果:
  1. None of the values is matching
  2. Exact value of a is: 100
转载本站内容时,请务必注明来自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号