Wednesday, 31 October 2012

Loops in VBScript



LOOPS IN VBScript:  To execute a block of code more than one time, we can use loops in VBScript

a)      WHILE LOOP:  To execute a block of code more than one time as long as given condition is true

----------------
---------------- (1)
----------------
                       While Condition
----------------       (2)
----------------        True (Loop)
  (3) False (Exit)               ----------------
                                                Wend
----------------
----------------
----------------
NOTE:  While loop ends with “Wend” (Not case sensitive)


b)      DOWHILE LOOP:  To execute a block of code more than one time as long as the condition is true, In this loop, the code will be executed at least once at the beginning without a condition

----------------
----------------
----------------
                                                    Do    (1)                  
----------------       (2)
----------------        True (Loop)
                              ----------------
                                                While condition
----------------  (3) False (Exit)
----------------
----------------

c)       DOUNTIL LOOP:  To execute a block of code more than one time as long as given condition is false, we can use this loop


----------------
----------------
----------------
                                                 Do (1)
----------------       (2)
----------------        False (Loop)
                              ----------------
                                                Until condition
----------------  (3) True (Exit)
----------------
----------------
NOTE:  DOUNTIL loop is otherwise around to DOWHILE loop

VBScript has an option of “EXIT DO” statement to terminate from “DOWHILE” and “DOUNTIL” loops if required


----------------
----------------
----------------
                                                   Do                            
----------------      
----------------       
                              ----------------
                                                If condition then
                                                Exit do

                            True (Exit)                               While condition

----------------   False (Exit)
----------------
----------------




d)      FORLOOP:  To execute a block of code for specified number of times, we can use this loop

EXP:
For i = 1 to 10 step 1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times

EXP:
For i = 0 to 9 step 1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times (here “0” counts as one loop)


EXP:
For i = 10 to 1 step -1

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 10 times


EXP:
For i = 1 to 10 step 3

                ------------------
                ------------------
                ------------------
                      Next (Exit)

The above loop executes 4 times (first execution is 1, then 4, then 7, and 10)

e)      FOREACH LOOP:  To run a block of code more than one time depends on each element in an array or list
EXP:                            
                X = array (123, “mindq”, “abc”, 789)
                                 X(0)       x(1)        x(2)     x(3)

------------------
                ------------------
                ------------------
Next (Exit)
This loop executes 4 times based on he values in X array
ARRAYS IN VBScript:  Variables are used to store one value at a time of any type, but arrays are used to store multiple values of any type.  To create arrays in VBScript, we can follow below code

Option explicit
Dim x(4)

In above example, X is an ARRAY with 5 values in it (0-4)

EXP:           x(0)      x(1)           x(2)         x(3)        x(4)
“abc”
123
“mindq”
10.5
“date”
       X=
From the above diagram, X is an array with 5 values or elements and each element will be accessible by using an array name and index, index starts with x(0)

EXP:  x(3)  = 4th element in “X” array

The first index x(0) is called LBOUND (lower bound)
The last index x(4) is called UBOUND (upper bound)

EXPAMPLE#1:  Write a VBScript code in QTP tool to create an array with 5 elements to store 5 subject marks into that array, and display total marks
Option explicit
Dim x(4), i, y
For i = 0 to 4 step 1
    x(i)=inputbox ("Enter a subject marks")
Next
    y=0
For i = 0 to 4 step 1
    y = y+x(i)
Next
    msgbox("Total Marks   "&y)

EXPAMPLE#2:  Write a VBScript code in QTP tool to create an array with 5 elements to store 5 Sentences, with at least one or two sentences with a word “INDIA”

Option explicit
Dim x(4), i
For i = 0 to 4 step 1
   x(i)=inputbox ("Enter a Sentence")
Next
For i = 0 to 4 step 1
If instr (x(i), "india")<>0 Then
   msgbox (x(i))
End If
Next

INSTR ( ):- This function is used to check the availability of sub string in main string

SYTAX:  instr(“mindq”, “testing”)
                (m-string)  (s-string)

     If available returns “position”
     If not available returns “0”

No comments:

Post a Comment