Introduction

Textbook Chapter 5: Programming with Control Structures

Decision control structures allow us to conditionally change the order in which a program is executed. Looping control structures allow us to repeat sections of code. Nested control structures make code easier to write and maintain.

Operators

Both VBScript and JavaScript support assignment, concatenation, arithmetic, comparison and unary operators. Assignment operators are used to assign a value or an object to a variable.

The assignment operator in both JavaScript and VBScript is the equal sign (=). JavaScript has additional assignment and arithmetic operators that are not supported in VBScript.

The concatenation operator is used to append expressions, such as a string and a variable. The concatenation operator in JavaScript is the plus sign (+), while the concatenation operator in VBScript is the ampersand (&).

Arithmetic Operators

Arithmetic operators allow you to perform mathematical calculations on values and variables. Because in JavaScript the arithmetic operator is the same key as the assignment operator, be careful that you do not append a string to a number. The number 1 and the string "1" are differentiated by the quotation marks. If you append a string to a number, for example 1 + a, the result will be the string "1a". The following table lists the arithmetic operators supported by both JavaScript and VBScript.

Operator     Description     Example     

+

Addition

x + y

-

Subtraction

x - y

*

Multiplication

x * y

/

Division

x / y

Notice how the parentheses change the order of operations in the following EXAMPLE.

Control Structures

There are two types of statements in VBScript and JavaScript:

  1. Action statements - code that carries out a task such as displaying text
    - steps are executed in the order in which they are written - known as linear programming
  2. Control statements - code that controls the execution order of scripts and action statements
    - allow you to conditionally determine whether and how an action statement is executed, and the order

Control structures use control statements to control the execution order of scripts and action statements. The benefits of using control structures include:

  • clearer and more readable Internet programming
  • less time spent debugging, testing and modifying the script
  • increased productivity
  • improved script quality, reliability and efficiency

Programmers often indent action statements within a control structure to make it easier to identify control statements.

The two types of control structures are:

  1. Decision Control Structures
  2. Loop Structures

Decision control structures allow you to alter the execution order of action statements on the basis of conditional expressions. A conditional expression is an expression that is evaluated by the scripting engine as true or false. Loop structures allow you to repeat action statements on the basis of conditional expressions.

Both types of control structures use control statements to alter the order in which action statements are executed. There are three types of control statements:

  1. Looping statements - allow you to conditionally repeat a block of code
  2. Branching statements - program decides which block of code to run, based on criteria in the control statement
  3. Jumping statements - allow you to conditionally exit a code block and jump to another section of code

Both JavaScript and VBScript support looping statements, branding statements and jumping statements. These three types of control statements can be used in both client-side and server-side scripting.

Algorithms and flowcharts make visually clear the sequence in which a script is executed. It is a good idea to plan and write out the order in which you want the script to be able to execute, before writing the code.