Visual Basic 算术运算符

算术运算符就是进行数学运算的运算符。主要有 +(加) 、-(减)、*(乘)、/(除)、%(取余)等。

下表显示了 VB 支持的所有算术运算符。假设变量 A=2,变量 B=7,则

运算符描述说明
^一个操作数的指定次幂值B^A = 49
+两个操作数相加A + B = 9
-第一个操作数减去第二个操作数A - B = -5
*两个操作数相乘A * B = 14
/第一个操作数除以第二个操作数B / A = 3.5
\第一个操作数除以第二个操作数的整数值B / A = 3
MOD模运算符,整数除法后的余数B / A = 1

实例

尝试下面的例子来理解 VB 的所有算术运算符:

  1. Module arithmetic_operators
  2. Sub Main()
  3. Dim a As Integer = 21
  4. Dim b As Integer = 10
  5. Dim p As Integer = 2
  6. Dim c As Integer
  7. Dim d As Single
  8. c = a + b
  9. Console.WriteLine("Line 1 - Value of c is {0}", c)
  10. c = a - b
  11. Console.WriteLine("Line 2 - Value of c is {0}", c)
  12. c = a * b
  13. Console.WriteLine("Line 3 - Value of c is {0}", c)
  14. d = a / b
  15. Console.WriteLine("Line 4 - Value of d is {0}", d)
  16. c = a \ b
  17. Console.WriteLine("Line 5 - Value of c is {0}", c)
  18. c = a Mod b
  19. Console.WriteLine("Line 6 - Value of c is {0}", c)
  20. c = b ^ p
  21. Console.WriteLine("Line 7 - Value of c is {0}", c)
  22. Console.ReadLine()
  23. End Sub
  24. End Module

结果如下:

  1. Line 1 - Value of c is 31
  2. Line 2 - Value of c is 11
  3. Line 3 - Value of c is 210
  4. Line 4 - Value of d is 2.1
  5. Line 5 - Value of c is 2
  6. Line 6 - Value of c is 1
  7. Line 7 - Value of c is 100

分类导航