You are on page 1of 31

VB Script for QTP

I) Introduction:
a) Scripting Languages VS Programming Languages
b) Basic features of VBScript.
c) Hosting Environments & Script Engines
d) Creating & running a script
e) Comments
II) Variables and Constants:
a) Vbscript data types
b) Assigning vaues to variabes
c) !ption E"picit Statement
d) #aming restrictions for variabes
e) Scope of Variabes
f) Array Variabes
g) $ynamic Arrays
%) $imensiona Arrays
i) Constants
III) Vbscript Operators:
a) !perator precedence.
b) Arit%metic operators
c) Comparison !perators
d) Concatenation operators
e) Logica operators
IV) Flow Control (a. Conditional Statements)
V) Flow Control (b. Looping through code)
1
Chapter- 1 (Introduction)
Scripting language vs programming anguages!
S.No Scripting languages Programming languages
1 These are interpreted based
languages.
These are Compiler based
languages.
2 These are light weight languages These are weightful languages.
3 Line by line eecution happens Total code at a time compilation
happens
!. "educes de#elopment time $ncreases de#elopment time
%. Creating user interface &'$( is
difficult in scripting languages.
Creating user interface &'$( is
easy in programming languages.
2
) *. Scripting language are + ,-.
/a#a Script. perl scrip. shell
script. python script. "uby script
etc.
* programming language are+
-asic.C0-0L. Portran. c. c1
1.,C11. /a#a. ,-. etc.
2 Scripting languages ha#e limited
support for 3raphics 4esign
Programming languages ha#e rich
Support for 3raphics 4esign.
Note+ Compilation+ Con#erting high5le#el language such as *nglish into
6achine le#el language e7ui#alence.
Note2+ compilation re7uired for programming languages not for scripting
languages.
Note 3+ $n order to achie#e any tas8 &S( less codes are enough in scripting
languages than programming languages.
II! VBScript Basic "eatures!
,- Script was launched in 1992 by 6icrosoft
$t is a lightweight #ersion of 6icrosoft ,- programming language.
,- script is a platform dependent language. &:or8s on windows
en#ironment li8e :indows client operating systems. windows ser#er
operating systems. internet eplorer. $nternet information ser#ice &$$S(
which is also 8now as 6icrosoft web ser#er.
$t has huge amount of operators support
$t supports file system operations. &Creating ;updating;#iewing;deleting
folders. flat files. word documents. P4<. =ml documents and *cel
Sheets.
$t supports data base operations.&4ata base connections. data #alidations.
4ata integrate . *TC.(
$t can be integrated with 6S >ccess. S?L Ser#er and oracle etc database
management system.
III Purposes of VBScript#Benefits of VBScript
$t has se#eral purposes. &$t is global concept(
3
$t is used in client side scripting in the web.&:e can embed ,-Script
into @T6L Pages. through this we can perform form #alidations. user
#alidations. etc. & $nternet eplorer pro#ides host en#ironment for
running ,-Script.(
$t can be used for ser#er side scripting in >SP &>cti#e Ser#er pages(
&$$S $nternet information ser#ice pro#ides host en#ironment for
running ,bscript.(
$t is used for Net wor8 >dministration.
$t is used for system administration.
$t supports file system and data base operations.
$t can be used for Test automation &?TP adapted ,- Script for Test
automation.
$t can be used for user interface de#elopment.
IV $S% ($indo& Script %ost)
:S@ is a 6icrosoft administration tool. :S@ creates host en#ironment for
running scripts. $t is built5in feature in :indows 9A and higher #ersions.
:S@ creates script host en#ironment on windows for ,-Script and /script
&6icrosoft&Scripts(. $f we want to run perl. python li8e scripts. we ha#e install
those scripting engines separately.
<or ,- Script
*n#ironment @ost en#ironment pro#ider
6s5 windows :S@
Client side &:*-( li8e form
#alidation Code in @T6L
$*
Ser#er side &:eb(code in >SP $$S
Test >utomation ?TP
V- Creating a Script
!
1. 0pen or launch or Start an editor &* notepad. ,-S *dit and @T6L
editor etc.(
2. Type the Script &eB 6sgbo C@ello,-ScriptD
3. Sa#e the tet file with &. ,bs etension( &$nstead of .tt etension(
!. Na#igate to the file u sa#ed and double clic8 it.
%. :S@ in#o8es the #b script engine and runs the script. > message bo is
displayed with the message C@ello ,- ScriptD
Chapter ' (Varia(les)
Varia(le) ,ariable is con#enient place holder that refers to a computer memory
location where we can store program information.
4ef 2+ ,ariable is a named location in computer memory where we can store
programming information.
,ariable stores in primary storage de#ice such as ">6 &"andom >ccess
memory(.
,ariable #alues may change during the time the script is running.
*eclaring Varia(les)
:e declare #ariables in our script using the 4$6. Public and pri#ate statements.
:e can declare #ariables eplicitly or implicitly.
*plicitly declaration $mplicitly declaration
4im . city
= E1FF
City ED@yderabadD
=E1FF
City E C@yderabadD

$mplicit declaration is simple but it is not a good practice.
+ption ,-plicit Statement)
%
$t forces eplicit declaration of all #ariables in a split.
The option eplicit declaration should be the first statement in our script.
0ption *plicit G forces declaring of all #ariables.
4im . city Gdeclaring #ariables
=E1FF Gdeclared #ariable
HE2FF Gundeclared #ariable generates error &if u obser#e in 4im = y is missing
that is why it shows error(
Note+ $f we use option eplicit statement it generates error for undeclared
#ariables. sometimes we may misspell #ariable names that time option eplicit
generates error. other wise script does not show any error and pro#ide proper
result.
.ssigning Values to Varia(les)
>. $nitiation+ >ssigning #alues directly in the script.
0ption eplicit
4im =. city
=E1FF
City E I@yderabadI
6sgbo I#alue is+ IJ
-. "eading+ "eading #alue from input de#ices.
*=+
0ption eplicit
4im
=E$nputbo &Ienter a #alueI(
6sgbo I,alue is + IJ =
Purposes of varia(les or (enefits of
varia(les!
<or storing results that generated by calculations in the script
*+ Total ETic8ets Kprice
<or storing 4ata that returned by functions
)
*+ Enow
msgbo
Now is a function we can also pass
a comment.
<or storing any program information.
City E CChenaiD
:e can declare multiple #ariables in a line
*+ dim a.b.c. city
0r
4im a
4im b
4im c
4im city
:e can assign #alues to #ariables in a line.
*=B
= E1FF
HE2FF
City EDhyderabadL
0r
=E1FF+yE2FF+city ED@yderabadD
/aming 0estrictions for varia(les)
1ust (egin &ith alpha(etic character)
2
,-) 23 43 cit43 25 is correct!
5-3 5 &rong!
Cannot contain em(edded period!
"or e-) 267!com &rong!
248com3 -4--com correct!
1ust not e-ceed to '99 characters
1ust (e uni:ue in the scope!
"or e-) Case 1) -34383 cit4 is correct!
Case ') -343-3cit4 ;&rong (&ith the same name it cannot create a
varia(le must (e uni:ue)
*ata T4pes)
,bscript does not ha#e eplicit data types. $t has only one data types called a
#ariant. $t is a special 8ind of data type that can contain different 8inds of
information. depending on how it is used.
<or con#erting data from one data type to another con#ersion functions
a#ailable in ,bscript.
*plicit declaration of 4ata types
*+ &C.c11 etc(
$mplicit declaration of 4ata types
&,-Script(
Char Name &trigun(
$nt Num &99(
<loat Num &12.!2(
Char;$nt;float;currency;date etc.
4im statement only.
Scope of Varia(les)
> #ariableLs scope is determined by the way we declare it.
:hen we declare a #ariable within a procedure. only code within the procedure
can access or change the #alue of that #ariable.
A
,-script
4im a. b. n
aE1FF+ bE2FF
nEa1b
6sgbo n
<unction mul
4im .y.res
E%F+ yE23
resEKa
mulEres
6sgbo Iresult is IJmul
*nd <unction
Call mul
tE1b
6sgbo t
Note1+ $n the abo#e eample a. b. n. t are script le#el #ariables. those can be
used in all procedures in the script.&we can use inside the procedure or outside
the procedure(
Note2+ $n the abo#e eample . y. res are procedure le#el #ariables. those can
be used in that procedure only. &$f we use procedure le#el #ariable in script
le#el ,bscript generates that #alue as Mero.(
T4pes of Varia(le)
,bscript has two types of #ariable.
1. Scalar #ariables + These contain a single #alue
2. >rray ,airbales+ These contain a series of #alues.
.rra4 Varia(les)
>rray #ariables and Scalar #ariables are declared in the same way. *pect that
the declaration of an array #ariable uses & ( following the #ariable name.
9
*+
4im a&3( >rray #ariable &4im a &3( stores ! #alues starts from Nero
4im b scalar #ariable &stores one #alue(
$n ,bscript arrays are Nero based.
.ssigning values to varia(les)
:e assign data to each of the elements of the array using an inde+
*+
4im a&3(
4im b
a&F(E1FF+ a&1(E2FF+ a&2(E3FF+ a&3(E3!%
bEa&2(
6sgbo b
*imensional .rra4s)
>rrays are not limited to single dimension. :e can ha#e as many as )F
dimensions. although most people canLt comprehend more than 3 or !
dimensions.
*=+
4im table &%.1F(
$n the abo#e eample the table #ariable is a 2 dimensional array consisting of )
rows and 11 columns.
$n a Two54imensional array. the first number is always a number of rowsB The
second number is the number of columns.
*4namic .rra4s)
:e can also declare an array whose siMe changes during the time our script is
running. This is called dynamic array.
*=+
4im myarray &(
"edim anotherarray &(
4im a.b. =&(
1F
"e4im &3(
&F(E1FF+ &1(E2FF+ &2(E3FF+ &3(E!FF
aE&3(
msgbo a
"e4im Preser#e &%(
&!(E999+ &%(EAAA
bE&2(1&%(
msgbo b
$n the abo#e eample 0edim sets initial #alue 3.
> subse7uent 0edim statement resiMes the array to %
:e ha#e to use Preserve 8eyword to preser#e the contents of the array as the
resiMing ta8es place.
/ote) There is no limit to number of times we can resiMe a dynamic array.
although if we ma8e an array smaller. we lost the data in the eliminated
elements.
Note + "edim is a statement and Preser#e is a 8eyword.
Comments
The comment or argument is the tet of any comment we want to include.
Purposes of Comments+
1. <or ma8ing the script;code understandable
2. <or ma8ing one or more statements disable for eecution.
S4nta-)
"em comment &after the rem 8eyword. a space is re7uired before comment(
0r
>postrophe &O( symbol before the comment
11
Comment#uncomment a (loc< of statements)
Select bloc8 of statements and use short cut 8ey Control 16 &for comment(
Select comment bloc8 and use short cut 8ey Control 1 Shift16 &for
uncomment(.
Constants)
> constant is a meaningful name that ta8es the place of a number or string and
ne#er changes.
Constant can be used for replacing literal &eact( #alues.
4eclaring Constants+
:e can create user5defined constants using Const statement.
*+
Const myname EDTrigunD
Const numberE1FF
Const mydate E P 1F51F599 P
:e can declare multiple constants by separating each constant name and #alue
with a comma.
*+
Const myname EDtrigunD. numE1FF. mydate E P1F51F599P
,ariable ,S Constantans
4im myname
myname EDTrigunD
6sgbo myname
myname EDpin8yD
6sgbo myname
Const myname EDTrigunD
6sgbo myname
myname EDpin8yD
K3enerates error
12
Chapter = (+perators)
0perators are used for performing mathematical. comparison and logical
operations.
,bscript has full range of operators including arithmetic operators. Comparison
operators. Concatenation operators and logical operators.
+perator Precedence)
:hen se#eral operations occur in an epression. each part is e#aluated and
resol#ed in a predetermined order called operator precedence.
:e can use &( parentheses to o#erride the order of precedence and force some
parts of an epression to be e#aluated before others.
:hen epressions contain operators from more than one category. arithmetic
operators are e#aluated first. comparison operators are e#aluated net and
logical operators are e#aluated last.
Comparison operators all ha#e e7ual precedence. this is they are e#aluated in
the left to right order.
>rithmetic and logical operators are e#aluated in the following order of
precedence.
1!.rithmetic +perators)
13
S!/o +perator *escription
1 *ponentiation &Q( "aises a number to the power of an
eponent. <or e &1FQ3(E1FFF
' 6ultiplication &K( 6ultiplies two numbers. <or e+
&1FK2F(E2FF
= 4i#ision &;( 4i#ides two numbers and returns a floating5
point results. <or e+ &1F;3(E3.3333333333
! $nteger di#ision &R( 4i#ides two numbers and returns an $nteger
result. <or e+ &1FR3(E3
9 6od operator 4i#ides two numbers and returns only the
reminder number. <or e+ 1F mod 3E1
> >ddition &1( Sums two numbers for e+ 1F1%E1%
? Subtraction &5( <inds the difference between two numbers
are indicates negati#e #alue of a numeric
epression. <or e+ 1F5% E %
@ Concatenation &J( <orces string concatenation of two
epressions &<or e+
trigunJpin8yEtrigunpin8y
'!Comparison +perators)
+perator *escription
1. *7ual to &E( 'sed to compare epressions
aE1FF
bE2FF
resE aEb
msgbo res
$t generates true;false
2. Not e7ual to
&ST(
aE1FF
bE2FF
resE aSTb
msgbo res
$t generates true;false
3. Less Than &S( aE1FF
bE2FF
resE aSb
msgbo res
$t generates true;false result
1!
!. 3reater than &T( aE1FF
bE2FF
resE aTb
msgbo res
$t generates true or false results
%. less than or
e7ual to &SE(
aE2F1
bE2FF
resE aSEb
msgbo res
$t generates true or false results
).3reater than or
e7ual to &TE(
aE2F1
bE2FF
resE aTEb
msgbo res
$t generates true or false results.
2. $s 0perator 0bUect e7ui#alence
=!Concatenation +perators)
+perator *escription
1. >ddition
operator &1(
a! $f both epressions are numeric then it adds
(! $f both epressions are strings then it concatenates.
c! +ne e-pression is numeric and the other is string
then it adds
2. Concatenation
operator &J(
<orces string concatenation of two epressions.
*+
aEItrigunI
bEIpin8yI
cEaJb
msgbo c
=! ogical +perators)
+perator *escription
1. Not Performs logical negation on an epression
2. >nd Performs a logical conUunction on two epressions.
aE1F
bE2F
1%
cE1%
$f aSb and cSb Then
msgbo Ib is big numberI
else
msgbo Ibig is not a big numberI
*nd $f
3. 0r Performs a logical disUunction on two epressions
aE1F
bE2F
cE2%
$f aSb or cSb Then
msgbo Ib is big numberI
else
msgbo Ibig is not a big numberI
*nd $f
!. or Performs a logical eclusion on two epressions.
aE1FF
bE2F
cE1%
$f aSb or cSb Then
msgbo IPassI
else
msgbo I<ailI
*nd $f
%. *7# Performs a logical e7ui#alence on two epressions.
aE1FF
bE2F
cE1%F
$f aSb e7# cSb Then
msgbo IPassI
else
msgbo I<ailI
*nd $f
). $mp Performs a logical implication on two numbers.
1)
IV) Flow Control (a. Conditional Statements)
:e can control flow of our script with conditional statements and Loop
statements.
'sing conditional statements. we can use ,bscript code that ma8es decisions.
$n ,bscript two types of conditional statements are a#ailable.
1. $fVthenVelse statement
2. Select case statement
Asage)
1! 0unning a statement if a condition is true (single statement)
4im mydate
mydateEP1F51F5FAP
msgbo mydate
$f mydate Snow Then mydate Enow
msgbo mydate
'! 0unning a (loc< of statements if condition is true (multiple statements)
4im .y
E1%
$f T1F Then
yE11FF
msgbo y
msgbo Itrigun is correctI
*nd $f
=! 0unning a (loc< of statements if a condition is true or running other
(loc< of statements if condition is false!
4im .y
E1FFF
yE2FF
$f Ty Then
msgbo I= is a big numberI
12
else
msgbo IH is a big numberI
*nd $f
B! *eciding among Several alternatives( if statement)
4im
Einputbo &Ienter a #alueI(
$f TF and SE1FF Then
msgbo I is a small numberI
else if T1FF and SE%FF Then
msgbo I is a medium numberI
else if T%FF and SE1FFF Then
msgbo I is a big numberI
else
msgbo I is a grand numberI
*nd $f
end if
end if
9! ,-ecuting a certain (loc< of statements &hen t&o or more conditions are
true! (/ested if)
4im state. region
state Einputbo &Ienter a stateI(
region Einputbo &Ienter a regionI(
$f stateEIapI Then
$f region EItelanganaI Then
msgbo Idist count is 1FI
else if region EIrayalasemaI Then
msgbo Idist count is !I
else if region EIcostalI Then
msgbo Idist count is 9I
1A
else
msgbo Iin#alid dataI
*nd $f
*nd $f
*nd if
*nd if
>! *eciding among Several alternatives (select case statement)
Synta+
Select case epression
Case CepressionD
Statements
555555555555
5555555555555
Case CepressionD
Statements
555555555555
5555555555555
Case else statements
5555555555555
55555555555555
*nd select
*+
0ption eplicit
4im .y.operation.res
Einputbo &Ienter #alueI(
yEinputbo &Ienter y #alueI(
operationEinputbo &Ienter an operationI(
Ecdbl&(
yEcdbl&y(
Select Case operation
19
Case IaddI
res E1y
msgbo Iadditon of .y is+ IJres
Case IsubI
res E5y
msgbo Isub of .y is+ IJres
Case ImulI
res EKy
msgbo Imul of .y is+ IJres
Case Idi#I
res E;y
msgbo Idi# of .y is+ IJres
Case IepoI
res EQy
msgbo Iepo of .y is+ IJres
case else
msgbo Iin#alid dataI
*nd Select
2F
V. Flow Control (a. Looping through code)
o Lopping ao&s us to run a group of statements
repeatedy.
o Some oops repeat statements unti a condition is true.
o !t%ers repeat statements unti a condition is fase.
o '%ere are aso oops t%at repeat statements a specific
number of times.
The ollowing looping statements are a!ailable in
Vbscript.
(. "o # Loop) Loops &%ie or unti a condition is true.
*. $hile# $end) Loops &%ie a condition is true.
+. For# %e&t) ,ses a counter to run statements a specific
number of times.
-. For each # %e&t: .epeats a group of statements for
eac% item in a coection or in an array.
'."o#Loop:
(. .epeating statements &%ie a condition is true.
Synta"()
$o &%ie condition
Statements
/ / / / / / / /
Loop
(& ':
$o &%ie " 01 "2"3(
21
msgbo" 4%i trigun4&"
Loop
(& ):
$o &%ie "01 "2"3(
5indo&647ig%t .eservation4).Activate
5indo&647ig%t .eservation4).5inButton64Button4).Cic8
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inC%ec8Bo"64!rder #o.4).Set 4!#4
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inEdit64Edit4).Set "
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inButton64!94).Cic8
Loop
Synta"*)
$o
Statements
/ / / / / / / /
Loop &%ie condition
(& ':
$o
msgbo" 4%i trigun4&"
"2"3(
Loop &%ie " 01
(& ):
"2(
$o
5indo&647ig%t .eservation4).Activate
5indo&647ig%t .eservation4).5inButton64Button4).Cic8
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inC%ec8Bo"64!rder #o.4).Set 4!#4
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inEdit64Edit4).Set "
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inButton64!94).Cic8
"2"3(
Loop &%ie "021
(& *:
$o ,nti " 2- "2"3(
22
msgbo" 4%i trigun4&:
Loop
*!+
$o
msgbo" 4%i trigun4&:
"2"3(
Loop ,nti " 2-
(& +:
$o unti "21 "2"3(
5indo&647ig%t .eservation4).Activate
5indo&647ig%t .eservation4).5inButton64Button4).Cic8
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inC%ec8Bo"64!rder #o.4).Set 4!#4
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inEdit64Edit4).Set "
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inButton64!94).Cic8
Loop
).$hile # wend statement
E"ecutes a series of statements as ong as given condition is true
Synta")
5%ie condition
Statements
//////////
5end
E"()
5%ie "01 "2"3(
msgbo" 4%eo %yderabad4&"
5end
E" *)
5%ie "01 "2"3(
5indo&647ig%t .eservation4).Activate
5indo&647ig%t .eservation4).5inButton64Button4).Cic8
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inC%ec8Bo"64!rder #o.4).Set 4!#4
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inEdit64Edit4).Set "
5indo&647ig%t .eservation4).$iaog64!pen !rder4).5inButton64!94).Cic8
5end
*. For %e&t Statement
"epeats a group of statements for a specific number of times.
23
<or counter Estart to end step &$ncrement(
Statements
WW55555555555555555555
Net
* 1+
<or E1 to % step 1
msgbo Ihello hyderabadIJ
Net
* 2+
<or E1 to % step 1
:indow&I<light "eser#ationI(.>cti#ate
:indow&I<light "eser#ationI(.:in-utton&I-uttonI(.Clic8
:indow&I<light "eser#ationI(.4ialog&I0pen 0rderI(.:inChec8-o&I0rder No.I(.Set I0NI
:indow&I<light "eser#ationI(.4ialog&I0pen 0rderI(.:in*dit&I*ditI(.Set
:indow&I<light "eser#ationI(.4ialog&I0pen 0rderI(.:in-utton&I0XI(.Clic8
Net
*. For (ach %e&t Statement

"epeats a group of statements of each element in an array or a collection
Synta+
<or each item in array
Statements
555555555555555
Net
* 1+
4im a.b. &3(
aE1F+ bE 2F
&F(EI>ddition of a.b is+ IJa1b
&1(EISub of a.b is+ IJa5b
&2(EI4i#ision of a.b is+ IJa;b
&3(EI6ul of a.b is+ IJaKb
<or each element in
msgbo element
Net
2!
* 2+
4im mycity
mycity Earray&IhyderabadI.ImumbaiI.I4elhiI. IchennaiI(
<or each element in mycity
msgbo element
Net
V. ,eneral (&amples.
E" ()
5rite a script for reading a number & finding out
&%et%er t%e given number is eap year or not;
$im num
num2inputbo" 64enter a number4)
<f 6num mod -)2= '%en
msgbo" 4t%is is a eap year4
ese
msgbo" 4t%is is not a eap year4
End <f
E" *) 5rite a program for reading & finding out
&%et%er a given number is even number or odd
number;
E" )
$im num
num2inputbo" 64enter a number4)
<f 6num mod *)2= '%en
msgbo" 4t%is is an even number4
ese
2%
msgbo" 4t%is is an odd number 4
End <f
E" +) dispay even numbers up to #;
$im num>n
n2inputbo" 64enter a number4)
7or num2* to n step *
msgbo" num
#e"t
E" -) dispay odd numbers up to #;
$im num>n
n2inputbo" 64enter a number4)
7or num2( to n step *
msgbo" num
#e"t
E" 1) .ead a number and find out engt% of t%at
number;
$im num
num2inputbo" 64enter a number4)
"2en6num)
msgbo" "
E" ?) $ispay #atura numbers in reverse order up to
#;
$im n
n2inputbo" 64enter a number4)
2)
7or num2 n to ( step /(
msgbo" num
#e"t
E") $im n
n2inputbo" 64enter a number4)
7or num2 (= to n step (=
msgbo" num
#e"t
Working with Arrays
Array Definition: A set of similar values is called an Array or List.
Declaration of Array in VBScript:
Option Explicit
Dim x , y(10)
where x is an a variable, y is an 11 elements array list
For arrays in VBScript, !" can ta#e memory se$%entially li#e shown below
10 1023 Testin A !"# 23$
%&0' (&1' (&2' (&3' (&$'
(&)'
L*ond
+*ond
!o store &ata to arrays, we can %se below co&e
Option Explicit
22
Dim x('), i
(or i ) 0 to ' step 1
x(i) ) inp%tbox(*Enter a val%e+)
,ext
O-
Option Explicit
Dim x
x ) array(1./, *test+,01234,+#+,1./'5401)

,(ample1: -rite VBScript in .T/ to create ) elements array0to store ) su*1ects mar2s and to
display total mar2s
34reate 5ive ,lements Array
Option Explicit
Dim mar#s('), i , tot
3!ead 5ive Su*1ect 6ar2s from 7ey*oard
(or i ) 0 to ' step 1
mar#s(i) ) inp%tbox(6Enter mar#s+)
,ext
35ind Total and display Total
tot)0
(or i ) 0 to ' step 1
tot ) tot 7 mar#s(i)
,ext
"rint( + !otal mar#s is + 8 tot)

2A
,(ample2: -rite VBScript in .T/ to create ) elements array 0to store ) su*1ects mar2s0and to
display 8i8est mar2s.
34reate 5ive ,lements Array
Option Explicit
Dim mar#s('), i , max
For i ) 0 to ' step 1
mar#s(i) ) inp%tbox (+ Enter 9ar#s+)
,ext
35ind ma(imum and display
max ) mar#s(0)
(or i ) 1 to ' step 1
:( cint(max) ; cint(mar#s(i)) !hen
max ) mar#s(i)
En& :(
,ext
"rint(+ 9axim%m mar#s is + 8max)

,(ample3: -rite VBScript in .T/ to create ) elemnts array0to store ) su*1ects mar2s and to
display ma(imum and minimum mar2s
34reate 5ive ,lements Array
Option Explicit
Dim mar#s('), i , max,min
For i ) 0 to ' step 1
mar#s(i) ) inp%tbox (+ Enter 9ar#s+)
,ext
35ind ma(imum and minimum and display *ot8 values
29
max ) mar#s(0)
min ) mar#s(0)
For i ) 1 to ' step 1
:( cint(max) ; cint(mar#s(i)) !hen
max ) mar#s(i)
En& :(
:( cint(min) < cint(mar#s(i)) !hen
min ) mar#s(i)
En& :(
,ext
"rint( + 9axim%m mar#s is *8 max 8+ 9inim%m mar#s is + 8 min)

,(ample$: -rite VBScript in .T/ to create ) elements array0to store ) employees Basic
salaries0 to calculate 9ross salaries *y addin commissions and to display ) employees ross
salaries after calculation.:ere commission is 10; on *asic salary <8en *asic salary
=>3000000 commission is ); on *asic salary0<8en *asic salary ?300000 and =>1)0000
commission is 200 <8en *asic salary ?1)000
Option Explicit
Dim sals(') , i , comm
3!ead 5ive ,mployees Basic Salaries from 7ey*oard
For i ) 0 to ' step 1
sals(i) ) inp%tbox( *Enter Basic Salary+)
,ext
35ind 4ommission @ 9ross and display
For i ) 0 to step 1
:( cln=(sals(i)) < /0000 !hen
3F
comm ) cln=(sals(i)) > 10?100
sals(i) ) cln=(sals(i)) 7 cln=(comm)
elsei( cln=(sals(i);/0000 an& cln=(sals(i))<)15000 !hen
comm ) cln=(sals(i)) > 5?100
sals(i) ) cln=(sals(i)) 7 cln=(comm)
else
comm ) .00
sals(i) ) cln=(sals(i)) 7 comm
En& :(
,ext
3Display 9ross Salaries
For i ) 1 to ' step1
"rint(*@ross Salary is + 8sals(i))
,ext
31

You might also like