This is a post where I will be placing all the hands on programming of QTP. I would stringly suggest every test engineers to go through this post at least once. This will also help you to clear you interview.
Running Certain Statements if a Condition is True and Running Others if a Condition is False
We can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.
Example:

Dim x x= Inputbox (" Enter a value") If x>100 Then Msgbox "X is a Big Number" Msgbox "X value is: "&X Else Msgbox "X is a Small Number" Msgbox "X value is: "&X End IfDeciding Between Several Alternatives A variation on the If...Then...Else statement allows us to choose from several alternatives. Adding ElseIf clauses expands the functionality of the If...Then...Else statement so we can control program flow based on different possibilities. Example:

Dim x x= Inputbox (" Enter a value") If x>0 and x<=100 Then Msgbox "X is a Small Number" Msgbox "X value is "&x Else IF x>100 and x<=500 Then Msgbox "X is a Medium Number" Else IF x>500 and x<=1000 Then Msgbox "X is a Large Number" Else Msgbox "X is a Grand Number" End If End If End IfExecuting a certain block of statements when two / more conditions are True (Nested If...) Example:

Dim State, Region State=Inputbox ("Enter a State") Region=Inputbox ("Enter a Region") If state= "WB" Then If Region= "Greater Kolkata" Then msgbox "Dist count is 5" Else if Region= "Hill" Then msgbox "Dist count is 4" Else If Region= "Costal" Then msgbox "Dist count is 3" End If End If End If End IfMaking Decisions with Select Case The Select Case structure provides an alternative to If...Then...ElseIf for selectively executing one block of statements from among multiple blocks of statements. A Select Case statement provides capability similar to the If...Then...Else statement, but it makes code more efficient and readable. Example:

Option explicit Dim x,y, Operation, Result x= Inputbox (" Enter x value") y= Inputbox ("Enter y value") Operation= Inputbox ("Enter an Operation") Select Case Operation Case "add" Result= cdbl (x)+cdbl (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 Operation" End SelectWrite 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 IfWrite 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 IfRead two numbers and display the sum?

Dim num1,num2, sum num1=inputbox ("Enter num1") num2=inputbox ("Enter num2") sum= Cdbl (num1) + Cdbl (num2) 'if we want add two strings conversion require msgbox ("Sum is " &sum)Read P,T,R values and Calculate the Simple Interest?

Dim p,t, r, si p=inputbox ("Enter Principle") t=inputbox ("Enter Time") r=inputbox ("Enter Rate of Interest") si= (p*t*r)/100 ' p= principle amount, t=time in years, r= rate of interest msgbox ("Simple Interest is " &si)Read Four digit number, calculate & display the sum of the number or display Error message if the number is not a four digit number?

Dim num, sum num=inputbox ("Enter a Four digit number") If Len(num) = 4 Then sum=0 sum=sum+num mod 10 num=num/10 num= left (num, 3) sum=sum+num mod 10 num=num/10 num= left (num, 2) sum=sum+num mod 10 num=num/10 num= left (num, 1) sum=sum+num mod 10 msgbox ("Sum is " &sum) else msgbox "Number, you entered is not a 4 digit number" End IfRead any Four digit number and display the number in reverse order?

Dim num,rev num= inputbox("Enter a number") If len(num)=4 Then rev=rev*10 + num mod 10 num=num/10 num= left(num,3) rev=rev*10 + num mod 10 num=num/10 num= left(num,2) rev=rev*10 + num mod 10 num=num/10 num= left(num,1) rev=rev*10 + num mod 10 msgbox "Reverse Order of the number is "&rev Else msgbox "Number, you entered is not a 4 digit number" End If7 Read 4 subjects marks; calculate the Total marks and grade? (a) If average marks Greater than or equal to 75, grade is Distinction b) If average marks Greater than or equal to 60 and less than 75 , then grade is First c) If average marks Greater than or equal to 50 and less than 60 , then grade is Second d) If average marks Greater than or equal to 40 and less than 50 , then grade is Third e) Minimum marks 35 for any subject, otherwise 'no grade fail')

Dim e,m,p,c, tot e=inputbox ("Enter english Marks") m=inputbox ("Enter maths Marks") p=inputbox ("Enter physics Marks") c=inputbox ("Enter chemistry Marks") tot= cdbl(e) + cdbl(m) + cdbl(p) + cdbl(c) msgbox tot If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=300 Then msgbox "Grade is Distinction" else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=240 and tot<300 Then msgbox "Grade is First" else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=200 and tot<240 Then msgbox "Grade is Second" else If cdbl(e) >=35 and cdbl(m) >=35 and cdbl(p) >=35 and cdbl(c) >=35 and tot >=160 and tot<200 Then msgbox "Grade is Third" else msgbox "No Grade, Fail" End If End If End If End IfWrite a code in QTP which will close a particular opened window in Desktop. Say i said only opened Notepads needs to be closed.

Set winobj=description.Create() Set objdesk=desktop.ChildObjects(winobj) cntobj=objdesk.count() msgbox cntobj sdt=Datatable.GetSheet("Global").GetParameter("OpenedApp") cntrow=DataTable.GetSheet("Global").GetRowcount() msgbox cntrow For i=1 to cntobj winapp1=objdesk(i).GetROProperty("text") If Winapp1=sdt Then ..... End If NextWritecode on calling an action say first action will take some input and will do some operation. Now the output of Action 1 will pass to Action 2 . The output of Action-2 will go to Action-3 . After finishing it will comeback to Action1 and give the final output. Action-1

a=inputbox("Enter a number:") b=inputbox("Enter a number:") isum=cint(a)+cint(b) RunAction "Action2", oneIteration,isum,output msgbox outputAction-2

imul=Inputbox("Enter a number to multiply:") pmul=Parameter("C")*imul Parameter("OUT")=pmul RunAction "Action3", oneIteration, pmul, out Parameter("OUT")=outAction-3

Parameter("LAST")=Parameter("IN3")+5 Split a string and find the length sinp=inputbox("Enter the Path:") strsplit=split(sinp,"\") slen=len(sinp) cnt=0 For i=1 to slen sms=mid(sinp,i,1) If sms="\" Then cnt=cnt+1 End If Next msgbox strsplit(cnt)Find a number as Odd or Even from a given set of input

sinp=inputbox("Enter the total no of numbers:") i=0 For i=1 to sinp sinp1= sinp & Inputbox("Enter the nos:") & "," Next msgbox sinp1 var1= split(sinp,",") For i=1 to sinp If var1(i) mod 2 <> 0 Then sodd=sodd & var1(i) & "," else seven=seven & var1(i) & "," End If Next msgbox sodd msgbox sevenFind out if a string is having numeric values or not

sinp=Inputbox("Enter the String::") slen=len(sinp) For i=1 to slen smid=mid(sinp,i,1) If isnumeric(smid) Then snum=snum & smid tfchk=True else tfchk=false End If Next If tfchk=True Then msgbox snum End IfFind the Dedcimal value of a given hexadecimal and vice varsa

sinp=Inputbox("Enter the Hexadecimal number::") slen=len(sinp) c=0 For i=1 to slen smid=mid(sinp,i,1) If isnumeric(smid)=False Then Select Case smid Case "A" n=10 Case "B" n=11 Case "C" n=12 Case "D" n=13 Case "E" n=14 Case "F" n=15 End Select c=c+cInt(n)*(16^(slen-i)) Else c=c+cInt(smid)*(16^(slen-i)) End If Next msgbox ("The Decimal Equivalentof the Hex number:: "&sinp&"=" &c)Swap two number or two input string

sinp1=Inputbox("Enter the First string:") sinp2=Inputbox("Enter the second string:") scon=sinp1&sinp2 msgbox scon lensinp1=len(sinp1) lensinp2=len(sinp2) lenscon=len(scon) msgbox lensinp1 msgbox lensinp2 msgbox lenscon 'For i=1 to lenscon 'smid1=mid(scon,i,lensinp1) 'Next 'msgbox smid1 srght=right(scon, lensinp2) msgbox "The swapped last name: " &srght sleft=left(scon, lensinp1) msgbox "The swapped first name: " &sleft scon2=srght&sleft msgbox "The swapped string:" & scon2

## No comments: