VBScript Tutorials - Herong's Tutorial Examples - Version 5.20, by Dr. Herong Yang

Introduction of Arithmetic Operations

This section provides a quick introduction of arithmetic operations supported by VBScript: addition, subtraction, multiplication, division, remainder, and exponentiation.

VBScript supports 8 arithmetic operations:

  • Addition (+): Returns the mathematical sum of both operands.
  • Subtraction (-): Returns the result of the first operand subtracted by the second operand.
  • Unary Negation (-): Returns the negative value of the only operand.
  • Multiplication (*): Returns the result of the first operand multiplied by the second operand.
  • Division (/): Returns the result of the first divided by the second operand.
  • Integer Division (\): Returns the result of the first operand divided by the second operand. Both operands are rounded to integer values before the operation. The decimal fraction part is removed from the resulting value.
  • Modulus (also called Remainder) (Mod): Returns the remainder of the first operand divided by the second operand. Both operands are rounded to integer values before the operation.
  • Exponentiation (^): Returns the first operand raised to the power of the second operand.

To show you some simple arithmetic operations, I wrote the following script, arithmetic_operation.html:

<html>
<body>
<!-- arithmetic_operation.html
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
-->
<pre>
<script language="vbscript">
   document.writeln("1 + 1 = " & (1 + 1))
   document.writeln("0 - 3 = " & (0 - 3))
   document.writeln("- 3 = " & (- 3))
   document.writeln("9 * 9 = " & (9 * 9))
   document.writeln("20 / 3 = " & (20 / 3))
   document.writeln("20 \ 3 = " & (20 \ 3))
   document.writeln("20 Mod 3 = " & (20 Mod 3))
   document.writeln("3 ^ 4 = " & (3 ^ 4))
</script>
</pre>
</body>
</html>

Here is the output of this VBScript example script:

1 + 1 = 2
0 - 3 = -3
- 3 = -3
9 * 9 = 81
20 / 3 = 6.66666666666667
20 \ 3 = 6
20 Mod 3 = 2
3 ^ 4 = 81

Table of Contents

 About This Book

 Introduction of VBScript - Visual Basic Scripting Edition

 Variant Data Type, Subtypes, and Literals

Arithmetic Operations

Introduction of Arithmetic Operations

 "+" - Arithmetic Addition Operation

 "-" - Arithmetic Subtraction Operation

 "*" - Arithmetic Multiplication Operation

 "/" - Arithmetic Division Operation

 "\" - Arithmetic Integer Division Operation

 "Mod" - Arithmetic Modulus Operation

 "^" - Arithmetic Exponentiation Operation

 Numeric Comparison Operations and Logical Operations

 String Operations - Concatenation and Comparison

 Variable Declaration and Assignment Statement

 Expression and Order of Operation Precedence

 Statement Syntax and Statement Types

 Array Data Type and Related Statements

 Array References and Array Assignment Statements

 Conditional Statements - "If ... Then" and "Select Case"

 Loop Statements - "For", "While", and "Do"

 "Function" and "Sub" Procedures

 Built-in Functions

 Inspecting Variables Received in Procedures

 Error Handling Flag and the "Err" Object

 Regular Expression Pattern Match and Replacement

 scrrun.dll - Scripting Runtime DLL Library

 Creating Your Own Classes

 IE Web Browser Supporting VBScript

 IIS ASP Server Supporting VBScript

 WSH (Windows Script Host)

 References

 Printable Copy - PDF Version

Introduction of Arithmetic Operations - Updated in 2015, by Dr. Herong Yang