VBScript
-----------------
i. VBScript is a scripting language.
ii. A scripting language is a lightweight programming language.
iii. VBScript is a light version of Microsoft’s programming language Visual Basic.
iv. Termination is not required.
v. Not a Case-Sensitive.
vi. & is Concatination Operator.
Declaring Variables
---------------------------
We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.
We can declare variables in two ways.
1. Implicit
2. Explicit
1. Implicit
---------------
Without declaring variables , we can store.
eg:
x=10
y=20
z=x+y
msgbox(z)
2. Explicit
---------------
Declaring variables using Dim or public or private.
eg:
Dim x,y,z
x=10
y=2
z=x+y
msgbox(z)
Note:
------------
By default QTP/UFT allows both implicit and explicit variables.
Option Explicit
---------------------------
Forces explicit declaration of all variables in a script.
Example:
Dim a,b,c
a=10
b=20
c=a+d
msgbox c
Note: Now you will get output 10. In above program we did't define 'd'. Now check above program using Option Explicit
Option Explicit
Dim a,b,c
a=10
b=20
c=a+d
msgbox c
Note: Now you will get an error, because you didn't declare a variable 'd'. Option Explicit doesn't allow undeclared variables.
* In VBScript only one datatype. i.e.Variant.
Variant:
------------
We can assign any value to the variable appropriate subtype will be given. It allows any kind of data.
Scalar Variables
-------------------------
A variable containing a single value is a scalar variable.
Array Variables
-------------------------
A variable containing a series of values, is called an array variable.
Eg: Dim A(2)
Although the number shown in the parentheses is 2, all arrays in VBScript are zero-based, so this array actually contains 3 elements.
We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 2, data can be assigned to the elements of an array as follows:
A(0) = 12
A(1) = 56
A(2) = 78
Operators
-------------------
1. Arithmetic Operators - + - * / \ Mod ^ &
2. Comparision Operators - > >= < <= = <>
3. Logical Operators - AND OR NOT
InputBox Function
-----------------------------------
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.
Eg:
Dim x
x = InputBox("Enter your name")
MsgBox ("You entered: " & x)
MsgBox Function
-----------------------------
Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.
eg:
Dim x
x = MsgBox ("Hello Tester!",0, "Uma")
Conditional Statements
=========================
We can control the flow of our script with conditional statements and looping statements.
Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:
1) If…Then…Else Statement
2) Select Case Statement
1. if...then
--------------
i. It used to execute block of statements
ii. It will execute the statement when condition is true
Syntax
---------------
If <conditon> Then
Statements
End If
Eg:
Dim a
a=inputbox("enter a value")
If a>20 Then
print a
End If
2. if..else
-----------------
i. Used to execute block of statements
ii. If the condition is true "if" part will execute
iii. if..the condition is false "else" part will execute
Syntax
-------------
If <condtion> Then
Statements
else
Statements
End If
Eg: Print greatest of two numbers
Dim a,b
a=inputbox("Enter a value")
b=inputbox("Enter b value")
If a>b Then
print "a is bigger :"&" "&a
else
print "b is bigger :"&" "&b
End If
3. nested..if
------------------------
It is used to check multiple conditions.
Syntax
-------------
If <conditon1> Then
Statements
elseif <condition2> then
Statements
elseif <condition n> then
Statements
End If
Eg: Print weekday name based on weekday number
Dim x
x=inputbox("Enter Weekday Number: ")
If x=1 Then
msgbox "Monday"
elseif x=2 then
msgbox "Tuesday"
elseif x=3 then
msgbox "Wednesday"
elseif x=4 then
msgbox "Thursday"
elseif x=5 then
msgbox "Friday"
elseif x=6 then
msgbox "Saturday"
elseif x=7 then
msgbox "Sunday"
else
msgbox "U have enterd wrong no"
End If
4. Select..Case
-------------------------
i. It is also used to execute block of statements to check multiple conditions and also replace with nested if
Syntax:
---------------
Select Case "<condition>/<Variable>"
Case "<Value1>"
Statements
Case "<value2>"
Statements
Case "<value n>"
Statements
Case else
Statements
End Select
Eg:Write a script to print debit card name based on
HDFC------VISA
ICICI ----Master
SBI-------mastro
Dim x
x=inputbox("enter a bank name ")
y=ucase(x)
Select Case y
Case "HDFC"
msgbox "debit card is"&":"& "VISA"
Case "ICICI"
msgbox "debit card is"&";"& "Master"
Case "SBI"
msgbox "debit card is"&";"& "Mastro"
Case else
msgbox "debit card is"&";"& " Null "
End Select
Eg: 2
------------
Option explicit
Dim x,y, Operation, Result
x= cint(Inputbox (" Enter x value"))
y=cint( Inputbox ("Enter y value"))
Operation= Inputbox ("Enter an Operation")
Select Case Operation
Case "add"
Result= x+y
Msgbox "Addition of x,y values is "&Result
Case "sub"
Result= x-y
Msgbox "Substraction of x,y values is "&Result
Case "mul"
Result= x*y
Msgbox "Multiplication of x,y values is "&Result
Case "div"
Result= x/y
Msgbox "Division of x,y values is "&Result
Case "mod"
Result= x mod y
Msgbox "Mod of x,y values is "&Result
Case "expo"
Result= x^y
Msgbox"Exponentation of x,y values is "&Result
Case Else
msgbox "Wrong Input"
End Select
1 Write a program for finding out whether the given year is a leap year or not?
Dim xyear
xyear=inputbox ("Enter Year")
If xyear mod 4=0 Then
msgbox "This is a Leap year"
Else
msgbox "This is NOT"
End If
2 Write a program for finding out whether the given number is, Even number or Odd number?
Dim num
num=inputbox ("Enter a number")
If num mod 2=0 Then
msgbox "This is a Even Number"
Else
msgbox "This is a Odd Number"
End If
-----------------
i. VBScript is a scripting language.
ii. A scripting language is a lightweight programming language.
iii. VBScript is a light version of Microsoft’s programming language Visual Basic.
iv. Termination is not required.
v. Not a Case-Sensitive.
vi. & is Concatination Operator.
Declaring Variables
---------------------------
We declare variables explicitly in our script using the Dim statement, the Public statement, and the Private statement.
We can declare variables in two ways.
1. Implicit
2. Explicit
1. Implicit
---------------
Without declaring variables , we can store.
eg:
x=10
y=20
z=x+y
msgbox(z)
2. Explicit
---------------
Declaring variables using Dim or public or private.
eg:
Dim x,y,z
x=10
y=2
z=x+y
msgbox(z)
Note:
------------
By default QTP/UFT allows both implicit and explicit variables.
Option Explicit
---------------------------
Forces explicit declaration of all variables in a script.
Example:
Dim a,b,c
a=10
b=20
c=a+d
msgbox c
Note: Now you will get output 10. In above program we did't define 'd'. Now check above program using Option Explicit
Option Explicit
Dim a,b,c
a=10
b=20
c=a+d
msgbox c
Note: Now you will get an error, because you didn't declare a variable 'd'. Option Explicit doesn't allow undeclared variables.
* In VBScript only one datatype. i.e.Variant.
Variant:
------------
We can assign any value to the variable appropriate subtype will be given. It allows any kind of data.
Scalar Variables
-------------------------
A variable containing a single value is a scalar variable.
Array Variables
-------------------------
A variable containing a series of values, is called an array variable.
Eg: Dim A(2)
Although the number shown in the parentheses is 2, all arrays in VBScript are zero-based, so this array actually contains 3 elements.
We assign data to each of the elements of the array using an index into the array.
Beginning at zero and ending at 2, data can be assigned to the elements of an array as follows:
A(0) = 12
A(1) = 56
A(2) = 78
Operators
-------------------
1. Arithmetic Operators - + - * / \ Mod ^ &
2. Comparision Operators - > >= < <= = <>
3. Logical Operators - AND OR NOT
InputBox Function
-----------------------------------
Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.
Eg:
Dim x
x = InputBox("Enter your name")
MsgBox ("You entered: " & x)
MsgBox Function
-----------------------------
Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.
eg:
Dim x
x = MsgBox ("Hello Tester!",0, "Uma")
Conditional Statements
=========================
We can control the flow of our script with conditional statements and looping statements.
Using conditional statements, we can write VBScript code that makes decisions and repeats actions. The following conditional statements are available in VBScript:
1) If…Then…Else Statement
2) Select Case Statement
1. if...then
--------------
i. It used to execute block of statements
ii. It will execute the statement when condition is true
Syntax
---------------
If <conditon> Then
Statements
End If
Eg:
Dim a
a=inputbox("enter a value")
If a>20 Then
print a
End If
2. if..else
-----------------
i. Used to execute block of statements
ii. If the condition is true "if" part will execute
iii. if..the condition is false "else" part will execute
Syntax
-------------
If <condtion> Then
Statements
else
Statements
End If
Eg: Print greatest of two numbers
Dim a,b
a=inputbox("Enter a value")
b=inputbox("Enter b value")
If a>b Then
print "a is bigger :"&" "&a
else
print "b is bigger :"&" "&b
End If
3. nested..if
------------------------
It is used to check multiple conditions.
Syntax
-------------
If <conditon1> Then
Statements
elseif <condition2> then
Statements
elseif <condition n> then
Statements
End If
Eg: Print weekday name based on weekday number
Dim x
x=inputbox("Enter Weekday Number: ")
If x=1 Then
msgbox "Monday"
elseif x=2 then
msgbox "Tuesday"
elseif x=3 then
msgbox "Wednesday"
elseif x=4 then
msgbox "Thursday"
elseif x=5 then
msgbox "Friday"
elseif x=6 then
msgbox "Saturday"
elseif x=7 then
msgbox "Sunday"
else
msgbox "U have enterd wrong no"
End If
4. Select..Case
-------------------------
i. It is also used to execute block of statements to check multiple conditions and also replace with nested if
Syntax:
---------------
Select Case "<condition>/<Variable>"
Case "<Value1>"
Statements
Case "<value2>"
Statements
Case "<value n>"
Statements
Case else
Statements
End Select
Eg:Write a script to print debit card name based on
HDFC------VISA
ICICI ----Master
SBI-------mastro
Dim x
x=inputbox("enter a bank name ")
y=ucase(x)
Select Case y
Case "HDFC"
msgbox "debit card is"&":"& "VISA"
Case "ICICI"
msgbox "debit card is"&";"& "Master"
Case "SBI"
msgbox "debit card is"&";"& "Mastro"
Case else
msgbox "debit card is"&";"& " Null "
End Select
Eg: 2
------------
Option explicit
Dim x,y, Operation, Result
x= cint(Inputbox (" Enter x value"))
y=cint( Inputbox ("Enter y value"))
Operation= Inputbox ("Enter an Operation")
Select Case Operation
Case "add"
Result= x+y
Msgbox "Addition of x,y values is "&Result
Case "sub"
Result= x-y
Msgbox "Substraction of x,y values is "&Result
Case "mul"
Result= x*y
Msgbox "Multiplication of x,y values is "&Result
Case "div"
Result= x/y
Msgbox "Division of x,y values is "&Result
Case "mod"
Result= x mod y
Msgbox "Mod of x,y values is "&Result
Case "expo"
Result= x^y
Msgbox"Exponentation of x,y values is "&Result
Case Else
msgbox "Wrong Input"
End Select
1 Write a program for finding out whether the given year is a leap year or not?
Dim xyear
xyear=inputbox ("Enter Year")
If xyear mod 4=0 Then
msgbox "This is a Leap year"
Else
msgbox "This is NOT"
End If
2 Write a program for finding out whether the given number is, Even number or Odd number?
Dim num
num=inputbox ("Enter a number")
If num mod 2=0 Then
msgbox "This is a Even Number"
Else
msgbox "This is a Odd Number"
End If
No comments:
Post a Comment