Home | | Internet & World Wide Web HOW TO PROGRAM | | Internet Programming | | Web Programming | Data Types and Control Structures - VBScript

Chapter: Internet & World Wide Web HOW TO PROGRAM - VBScript

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Data Types and Control Structures - VBScript

VBScript has only one data type—variant—that is capable of storing different types of data (e.g., strings, integers, floating-point numbers etc.).

Data Types and Control Structures

 

VBScript has only one data type—variant—that is capable of storing different types of data (e.g., strings, integers, floating-point numbers etc.). The data types (or variant subtypes) a variant stores are listed in Fig. 24.4. VBScript interprets a variant in a manner that is suit-able to the type of data it contains. For example, if a variant contains numeric information, it will be treated as a number; if it contains string information, it will be treated as a string.

 

Variable names cannot be keywords and must begin with a letter. The maximum length of a variable name is 255 characters containing only letters, digits (0–9) and underscores. Variables can be declared simply by using their name in the VBScript code. The statement Option Explicit can be used to force all variables to be declared before they are used.


VBScript provides control structures (Fig. 24.5) for controlling program execution. Many of the control structures provide the same capabilities as their JavaScript counter-parts. Syntactically, every VBScript control structure ends with one or more keywords (e.g., End If, Loop, etc.). Keywords delimit a control structure’s body—not curly braces (i.e., {}, as in JavaScript).

 

The If/Then/End If and If/Then/Else/End If control structures behave identi-cally to their JavaScript counterparts. VBScript’s multiple selection version of If/Then/ Else/End If uses a different syntax from JavaScript’s version because it includes key-word ElseIf (Fig. 24.6).

 

Notice that VBScript does not use a statement terminator like the semicolon (;) in JavaScript. Unlike in JavaScript, placing parentheses around conditions in VBScript is optional. A condition evaluates to True if the variant subtype is boolean True or if the variant subtype is considered non-zero. A condition evaluates to False if the variant sub-type is boolean False or if the variant subtype is considered to be 0.

 

VBScript’s Select Case/End Select structure provides all the functionality of JavaScript’s switch structure, and more (Fig. 24.7).



 


Notice that the Select Case/End Select structure does not require the use of a statement like break. One Case cannot accidentally run into another. The VBScript Select Case/End Select structure is equivalent to VBScript’s If/Then/Else/End If multiple selection structure. The only difference is syntax. Any variant subtype can be used with the Select Case/End Select structure.

VBScript’s While/Wend repetition structure and Do While/Loop behave identi-cally to JavaScript’s while repetition structure. VBScript’s Do/Loop While structure behaves identically to JavaScript’s do/while repetition structure.

 

VBScript contains two additional repetition structures, Do Until/Loop and Do/ Loop Until, that do not have direct JavaScript equivalents. Figure 24.8 shows the closest comparison between VBScript’s Do Until/Loop structure and JavaScript’s while structure. The Do Until/Loop structure loops until its condition becomes True. In this example, the loop terminates when x becomes 10. We used the condition !( x == 10 ) in JavaScript here, so both control structures have a test to determine whether x is 10. The JavaScript while structure loops while x is not equal to 10 (i.e., until x becomes 10).

 

Figure 24.9 shows the closest comparison between VBScript’s Do/Loop Until structure and JavaScript’s do/while structure. The Do/Loop Until structure loops until its condition becomes True. In this example, the loop terminates when x becomes 10. Once again, we used the condition !( x == 10 ) in JavaScript here so both control struc-tures have a test to determine if x is 10. The JavaScript do/while structure loops while x is not equal to 10 (i.e., until x becomes 10).

 

Notice that these Do Until repetition structures iterate until the condition becomes True. VBScript For repetition structure behaves differently from JavaScript’s for repe-tition structure. Consider the side-by-side comparison in Fig. 24.10.

 

Unlike JavaScript’s for repetition structures condition, VBScript’s For repetition structure’s condition cannot be changed during the loop’s iteration. In the JavaScript for/ VBScript For loop side-by-side code comparison, the JavaScript for loop would iterate exactly two times, because the condition is evaluated on each iteration. The VBScript For loop would iterate exactly eight times because the condition is fixed as 1 To 8—even though the value of x is changing in the body. VBScript For loops may also use the optional Step keyword to indicate an increment or decrement. By default, For loops increment in units of 1. Figure 24.11 shows a For loop that begins at 2 and counts to 20 in Steps of 2.



The Exit Do statement, when executed in a Do While/Loop, Do/Loop While, Do Until/Loop or Do/Loop Until, causes immediate exit from that structure. The fact that a Do While/Loop may contain Exit Do is the only difference, other than syntax, between Do While/Loop and While/Wend. Statement Exit For causes immediate exit from the For/Next structure. With Exit Do and Exit For, program execution con-tinues with the first statement after the exited repetition structure.

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.