You are on page 1of 13

Comparing VBScript control structures to Comparing VBScript control structures to

JavaScript control JavaScript control structures structures


http://www.apextgi.in
Data Types and Control Structures Data Types and Control Structures
JavaScript VBScript
1 if ( s == t )
2 u = s + t;
3 else if ( s > t )
4 u = r;
5 else
6 u = n;
1 If s = t Then
2 u = s + t
3 ElseIf s > t Then
4 u = r
5 Else
6 u = n
7 End If


JavaScript VBScript
1 switch ( x )
2 c!se 1"
3 !lert(#1#);
4 $re!%;
5 c!se 2"
6 !lert(#2#);
7 $re!%;
& def!ult"
' !lert(#(#);
1) *
1 +elect ,!se x
2 ,!se 1
3 ,!ll -s./0x(#1#)
4 ,!se 2
5 ,!ll -s./0x(#2#)
6 ,!se Else
7 ,!ll -s./0x(#(#)
& End +elect

Comparing JavaScripts if structure to VBScripts If structure
Comparing JavaScripts switch to VBScripts Select Case
Data Types and Control Structures
Data Types and Control Structures
JavaScript VBScript
1 while ( 1( x == 1) ) )
2 ++x;
1 20 3ntil x = 1)
2 x = x + 1
3 4005


JavaScript VBScript
1 d0
2 ++x;
3 * while ( 1( x == 1) ) );
1 20
2 x = x + 1
3 4005 3ntil x = 1)


JavaScript VBScript
1 x = &;
2 f0r ( 6 = 1; 6 7 x; 6++ )
3 x 8= 2;
1 x = &
2 90r 6 = 1 T0 x
3 x = x : 2
4 ;ext

Comparing JavaScripts while to VBScripts 20 3ntil
Comparing JavaScripts d08while to VBScripts 20 400583ntil
Comparing JavaScripts for to VBScripts For
Data Types and Control
Data Types and Control
Structures
Structures
Select Case/End Select

Does not require break type statement


VBScript structures without direct JavaScript equivalents:

Do Until/Loop

Do/Loop Until

Loop until condition becomes True


Exit Do

mmediate e!it "rom Do hile/Loop# Do/Loop hile# Do


Until/Loop or Do/Loop Until
Exit !or

mmediate e!it "rom !or/"ext


!or loop

$ptional Step %eyword to increment or decrement


# $ %&Script
' !or y ( ' To ') Step '
* Call +sg&ox, -y ( - . y /
0 "ext
%&Script !unctions
%&Script !unctions

&rede"ined "unctions
Variant "unctions

1sE2pty

'eturns True i" variant not initiali(ed


)ath "unctions

Cos# Sin# etc*

+a%e arguments in radians

radians , degrees -./01


1nput&ox

Displays dialog in which user can input data


+sg&ox

Displays message dialog


VBScript "unctions o"ten ta%e optional arguments
2ormatting "unctions

!or2atCurrency# !or2atDateTi2e# etc*


%&Script !unctions
%&Script !unctions
2unctions "or getting in"o about scripting engine

ScriptEngine

'eturns 33script4# 3%&Script4 or 3%&44

ScriptEngine&uild%ersion

'eturns current build version5 D number "or current release

ScriptEngine+a5or%ersion

'eturns ma6or version number "or script engine

ScriptEngine+inor%ersion

'eturns minor release number

Line continuation character


7nderscore character# 6
Statements cannot e!tend beyond current line without character
%&Script Exa2ple 7rogra2s
%&Script Exa2ple 7rogra2s

8lways place VBScript code inside 9+)L comments


&revent code "rom being displayed as te!t in browsers that do
not understand VBScript

Script variables
Variables declared outside o" procedures

Const %eyword
Create constants
%&Script Exa2ple 7rogra2s %&Script Exa2ple 7rogra2s

Comments
Single quote :8;
<eyword 9e2 :remar%;

Considered outdated

&rocedures:
Sub %eyword

&rocedure that does not return a value

Exit Sub e!its Sub procedure


!unction %eyword

&rocedure that returns a value

Exit !unction e!its !unction procedure


%&Script Exa2ple 7rogra2s
%&Script Exa2ple 7rogra2s

2unction 1nput&ox
Displays a dialog "or user to input data
Call 1nput&ox , pro2pt: caption: help !ile:6
x;coord: y;coord/
Coordinates measured "rom top le"t :1#1;*

)easured in twips :/==1 twips , / inch;


$ptional parameter help !ile can be le"t out by writing
consecutive commas # #
6 :underscore; > line continuation character# required i" statement
e!tends beyond a line

7se as many as necessary


%&Script Exa2ple 7rogra2s
%&Script Exa2ple 7rogra2s

Calling "unctions
" "unction call has arguments in parentheses# use %eyword
Call

" "unction assigns a variable# Call not needed


a ( 4bs, < /

" parentheses not used# %eyword Call not needed

2unction +sg&ox
Displays message dialog
+sg&ox -%&Script is =un>-: : -9esults-
Displays -%&Script is =un>- with -9esults- in the title
bar
$ptional argument to customi(e buttons and icon ignored
1.1Set language to
VBScript
1.2<5ti0n Ex5licit
statement
1.3Define procedure <n
,lic% for the c=>dd
button
1.4Use ,Int to convert
input values from
string subtype to
integer subtype
1712<,T?@E AT-4 @3/4I, #B88C3,882T2 AT-4 4D)
Tr!nsiti0n!l88E;#> 27AT-4>
371BB9i.D 22D15" !dditi0nDht=l BB>
57AE>2>
67TIT4E><ur first E/+cri5t78TIT4E>
7
&7+,FI@T 4>;G3>GE = #E/+cri5t#>
'71BB
1) <5ti0n Ex5licit
11 2i= intT0t!l
12
13 +u$ c=d>ddH<n,lic%()
14 2i= intE!lue
15
16 intE!lue = In5ut/0x(#Enter !n inte.er#I #In5ut
/0x#I I H 17 1)))I 1))))
1& intT0t!l = ,Int( intT0t!l ) + ,Int( intE!lue )
1' ,!ll -s./0x(#?0u entered # J intE!lue J H
2) #; t0t!l s0 f!r is # J intT0t!lI I
#Fesults#) 21 End +u$
22BB>
2378+,FI@T>
2478AE>2>
25
267/<2?>
27,lic% the $utt0n t0 !dd !n inte.er t0 the t0t!lD
2&7AF>
2'79<F->
3)7I;@3T ;>-E = #c=d>dd# T?@E = #/3TT<;#
31 E>43E = #,lic% Aere t0 >dd t0 the T0t!l#>
32789<F->
3378/<2?>
3478AT-4>
Adding integers on a Web page using VBScript Adding integers on a Web page using VBScript
nput dialog
)essage dialog
"aceboo%*com.ape!*tgi
twitter*com.8pe!tgi?oida
pinterest*com.ape!tgi
Stay Connected with us =or 2ore chapters on %& Script and 34%4
Script
http://www.apextgi.in

You might also like