VBScript Part-2

Looping Statements
==================
i. Looping allows us to run a group of statements repeatedly.
ii. Some loops repeat statements until a condition is False.
iii. Others repeat statements until a condition is True.
There are also loops that repeat statements a specific number of times.

Types of Loops
------------------------
1. Do…Loop
2. While…Wend
3. For…Next
4. For Each…Next


1. While…Loop
-------------------
i. It is used to execute block of statements for multiple iterations.
ii. When the condition is true, then it executes statements.
iii. Once the conditon is false it will come out from loop
iv. It is called pre-conditional statement.

Syntax:
----------------
While <conditon>
    Statements
Wend

Eg: Printing 1 to 10 Numbers
---------------------------------------------
i=1
While i<=10
print i
i=i+1
Wend

Eg: Print even numbers up to 30
----------------------------------------------
i=1
While i<=30

If (i mod 2=0)  Then
    print i

End If
i=i+1
Wend

Eg;
---------
Dim x
x=0
While  x<5  x=x+1
     print "Hello UFT"
Wend


2. do..Loop
--------------------
i. It is  flexible loop used to execute a block of statements
ii. It returns true when conditin is true or false
iii. It is post-conditional statement.

do..while
Do until

Eg: Print numbers up to 100 Exit loop when it reachs 25
--------------
i=1
Do while i<=100
print i

If i=25 Then
Exit do
End If
    i=i+1
Loop


3. For ..loop
--------------------
It is used to execute block of statement for specified no of iteration

Syntax:
---------------
For <conter variable>=<initial value> to <end value> step <no>
    Statements

if..we want to exit for then specify
Exit for

Next

Eg: Print odd numbers from 1 to 10
------------------------
For i=1 to 10 step 2
     print i
Next

Eg: Print even numbers from 2 to 20
------------------------
For i=1 to 20
      If (i mod 2 =0) Then
print i
      End If
Next



 For with Exit
----------------------
For i=2 to 20 step 2
   print i

If i=10 Then
   Exit for
End If
Next


4. for .. each
-------------------------
It is use to return  every element in an array

Syntax:
----------------
For each <element> in <arrrayname>
    print <element>
Next

Eg:
---------------
x=array(10,20,30,40)
For each i in x
    print i
Next

Eg:
------------
x=array("IBM","Deloitte","wipro")
For each i in x
print i
Next

Eg:
---------------
Dim a,b,x (3)
a=20
b=5
x(0)= "Addition is "& a+b
x(1)="Substraction is " & a-b
x(2)= "Multiplication is  " & a*b
x(3)= "Division is " &  a/b

For Each element In x
       msgbox element
Next





VB Script Built in Functions
============================
1) Abs Function
------------------------
Returns the absolute value of a number.

Eg:
Dim num
num=abs(-50.33)
msgbox num


2) Array Function
--------------------------
Returns a variant containing an Array

Eg:

Dim A
A=Array("UFT","Load Runner","Informatica")
msgbox A(0)
ReDim A(5)
A(4)="Delhi"
msgbox A(4)


3) Asc Function
-----------------------
Returns the ANSI character code corresponding to the first letter in a string.
Eg:

Dim num
num=Asc("A")
msgbox num


4) Chr Function
-------------------------
Returns the character associated with the specified ANSI character code.

EG:
Dim char
Char=Chr(65)
msgbox char

5) CInt Function
------------------------------
Returns an expression that has been converted to a Variant of subtype Integer.

Eg:

Dim num
num=123.45
myInt=CInt(num)
msgbox MyInt

6) Date Function
---------------------------
Returns the Current System Date.

Eg: 1

Dim mydate
mydate=Date
msgbox mydate

Eg: 2

Dim myday
mydate=date
myday=Day(Mydate)
msgbox myday

9) Hour Function
---------------------------
Returns a whole number between 0 and 23, inclusive, representing the hour of the day.

Eg:
Dim mytime, Myhour
mytime=Now
myhour=hour (mytime)
msgbox myhour

10) Join Function
--------------------------------
Returns a string created by joining a number of substrings contained in an array.

Eg:

Dim mystring, myarray(3)
myarray(0)="Automation"
myarray(1)="test"
myarray(2)="hub"
mystring=Join(MyArray)
msgbox mystring

11) Time Function
------------------------------
Returns a Variant of subtype Date indicating the current system time.

Eg:
Dim mytime
mytime=Time
msgbox mytime

12)  isNumeric Function
-------------------------------------
It Returns True/False like Result

Eg:

Dim MyVar, MyCheck
MyVar = 53
MyCheck = IsNumeric(MyVar)
Msgbox MyCheck
MyVar = "459.95"
MyCheck = IsNumeric(MyVar)
msgbox MyCheck
MyVar = "45 Help"
MyCheck = IsNumeric(MyVar)
msgbox MyCheck

String Functions
======================
1. Ucase :  It is used to convert the given string to uppercase
-------------
Syntax:

Ucase("<string>")

Eg:

Dim x
x="uma"
msgbox ucase(x)

2. Lcase :  It is used to covert the given sting to lower case
---------------
Syntax:

Lcase(<string>)

Eg:

Dim x
x="SUNDAy"
msgbox Lcase(x)


3.Ltrim : It is used to remove leading space of a given string
--------------

Removes space from LHS(left hand side)

Syntax:

Ltrim(<string>)

Eg:

Dim x
x="                      QTP"
msgbox ltrim(x)

4. Rtrim :  It is used to remove triling space of a string
---------------
Removes space from RHS

Syntax:

Rtrim(<string>)

Eg:

Dim x
x="QTP                               "
msgbox Rtrim(x)


5. Trim : It is used to remove both leading and triling space of a string
-----------
Syntax:
trim(<string>)

Eg:

Dim x
x="                  QTP                        "
msgbox trim(x)

6. len : It is used to return the lenght of a string
-----------------
Syntax:
len(<string>)

Eg:

Dim x
x="              ARMY"
msgbox len(x)

7. strreverse : It is used to revese the givn string

Syntax:

strreverse("<string>")

Eg:

Dim x
x="uma"
msgbox strreverse(x)


8. Write a script to check weather the given string is Polindrome or not

MADAM
DAD
LIRIL

Eg:

x=inputbox("Enter the Name:  ")
y=strreverse(x)
If x=y Then
msgbox "Yes"
else
msgbox "No"
End If

9. cint : It is used to covert string to integer

Eg:

a="10"
b="20"
c=cint(a)+cint(b)
msgbox c

9. cdbl : It converts into float

Option explicit
Dim a,b,c
a="10.3"
b="20.1"
c=cdbl(a)+cdbl(b)
msgbox c

10. Split :
----------------
 It is used to divide the string base on delemeter delemeter mans spacial char(like ,.^& @ #..)

Syntax:

split(expression,delemeter)

Eg:
Dim x,sp
x="IBM;Deloitte;Wipro;TCS"
sp=split(x,";")
For each i in sp
   print i
Next

11. Date: It is used to display current date of a system
------------
msgbox date

12. time : It is used to display the current time of a system
--------------
msgbox time

msgbox date &" "& time

13. cdate : Converts in to system date  format
--------------
Syntax:

cdate("expresion")

Eg:

x="jan 2 2006"
msgbox cdate(x)

15.Datepart : It is used to return dates in parts
--------------
Syntax:

datepart(interval,date)

msgbox datepart("m",date)
------months
msgbox datepart("d",date)
------days
msgbox datepart("yyyy",date)
----year
msgbox datepart("q",date)
----quarter

16.Left : It is used to return the no of charector from left side of a string
---------------
Syntax:
left(sting,length)

Eg:
x="this is sam"
msgbox left(x,4)

17. Right: It is used to return the no of charector from Right side of a string
------------
Syntax:

Right(string,length)

Eg:
x="This is SAM"
msgbox right(x,3)

18. mid :
-------------
It is used to return specified portion of a string based on start position and length

Syntax:

mid(sting,start position,length)

Eg:
x="this is sam"
msgbox  mid(x,6,2)

19. instr
------------------
msgbox instr("QTP","T")


No comments:

Post a Comment

PHP Notes

The Characteristics of PHP:- ----------------------------------- 1.PHP is a high level programming language. 2.It is a server-side scrip...