Vb.net-arithmetic-operators

提供:Dev Guides
移動先:案内検索

VB.Net-算術演算子

次の表は、VB.Netでサポートされているすべての算術演算子を示しています。 変数 A が2を保持し、変数 B が7を保持すると仮定します-

Operator Description Example
^ Raises one operand to the power of another B^A will give 49
+ Adds two operands A + B will give 9
- Subtracts second operand from the first A - B will give -5
* Multiplies both operands A* B will give 14
/ Divides one operand by another and returns a floating point result B/A will give 3.5
\ Divides one operand by another and returns an integer result B \ A will give 3
MOD Modulus Operator and remainder of after an integer division B MOD A will give 1

VB.Netで利用可能なすべての算術演算子を理解するために、次の例を試してください-

Module operators
   Sub Main()
      Dim a As Integer = 21
      Dim b As Integer = 10
      Dim p As Integer = 2
      Dim c As Integer
      Dim d As Single

      c = a + b
      Console.WriteLine("Line 1 - Value of c is {0}", c)

      c = a - b
      Console.WriteLine("Line 2 - Value of c is {0}", c)

      c = a * b
      Console.WriteLine("Line 3 - Value of c is {0}", c)

      d = a/b
      Console.WriteLine("Line 4 - Value of d is {0}", d)

      c = a \ b
      Console.WriteLine("Line 5 - Value of c is {0}", c)

      c = a Mod b
      Console.WriteLine("Line 6 - Value of c is {0}", c)

      c = b ^ p
      Console.WriteLine("Line 7 - Value of c is {0}", c)
      Console.ReadLine()
   End Sub
End Module

上記のコードをコンパイルして実行すると、次の結果が生成されます-

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