VBScript Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 4.01

Expressions

VB Script Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Data Types and Literals

Variables

Logic Operations

String Operations

Conditional Statements

Arrays

Loop Statements

Functions and Subroutines

Built-in Functions

Variable Inspection

... Table of Contents

This chapter describes:

  • What Is an Expression?
  • Precedence of Operations
  • Expression Example

Notes and samples in this chapter are based Visual Basic 6.0.

What Is an Expression?

Giving a precise single definition of an expression is not an easy task. So I will try to define it in a recursive way:

1. A simple expression is a presentation of a data value like, a literal, a variable, an element of an array, or a function call.

2. A complex expression is an operation represented by an operator, and one or two expressions as operands. The operation will result a data value.

If you apply rule #2 recursively, an expression may contain multiple operations in a sequence. When this happens, operations must be carried out in an order defined by the following rules:

A. Operations enclosed in a pair of parentheses must be carried out before operations outside the parentheses.

B. Operations with higher precedence must be carried out before operations with lower precedence.

C. Operation on the left must be carried out before the operation on the right side.

D. Rule A must be applied before Rule B, which must be applied before Rule C.

Precedence of Operations

The following table shows you the relative precedence of some commonly used operations:

Precedence   Operations       Notes
1            ^                Exponentiation
2            * / \ Mod        Multiplication, division, ... 
3            + -              Addition and subtraction
4            = <> < > <= >=   Comparisons
5            And Or Xor       Logical operations

There is nothing new in this table. We should have learned this in high school classrooms.

Expression Example

Now let me try to show you some expressions in an example, expression.html:

<html>
<body>
<!-- expression.html
   Copyright (c) 2006 by Dr. Herong Yang. http://www.herongyang.com/
-->
<pre>
<script language="vbscript">
'  Expressions with a single data literal
   document.writeln("")
   document.writeln(777)
   document.writeln(0.00314159e30)
   document.writeln(TRUE)

'  Expressions with a single operation
   document.writeln("")
   document.writeln(3.333e200 + 0.111e200)
   document.writeln(11.0 * 1.0e20)
   document.writeln(1 = 1)
   document.writeln(9.9999e-1 < 1)
   document.writeln(True Or False)

'  Expressions with multiple operations
   document.writeln("")
   document.writeln(+ 1 - 2 * 3 / 4)
   document.writeln(+ 1 - 2 * 3 / 4 = ( + 1 - 2 ) * 3 / 4)
   document.writeln(1 - 2 * 3 / 4 = (1 - 2) * 3 / 4 And 0.9999 < 1)
</script>
</pre>
</body>
</html>

Here is the output:


777
3.14159E+27
True

3.444E+200
1.1E+21
True
True
True

-0.5
False
False

All results look correct to me.

Conclusions

  • Expression always results a single value.
Dr. Herong Yang, updated in 2006
VBScript Tutorials - Herong's Tutorial Notes - Expressions