Vb scripting

  • 1,930 views
Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,930
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
97
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. VBScript Session 5 D ani V ainstein
  • 2. What we learn last session?
    • Branching
    • Branching using If … Then … Else statement.
    • Branching using Select Case
    Dani Vainstein
  • 3. Subjects for session 5
    • Looping through code.
      • Do … Loop
      • While … Wend
      • For … Next
      • For … Each
    • Exit statement.
    Dani Vainstein
  • 4. Looping Through Code
    • Looping allows you to run a group of statements repeatedly.
    • Some loops repeat statements until a condition is False ;
    • others repeat statements until a condition is True .
    • There are also loops that repeat statements a specific number of times.
    Dani Vainstein
  • 5. Do While/Until Dani Vainstein
  • 6. Looping Through Code Using Do…Loop Statement
    • Repeats a block of statements while a condition is True or until a condition becomes True .
    • The following looping statements are available in VBScript:
    Dani Vainstein
      • Do [{ While | Until } condition ]    
        • [ statements ]    
        • [ Exit Do ]    
        • [ statements ]
        • Loop
      • Do     [ statements ]   
        • [ Exit Do ]    
        • [ statements ]
        • Loop [{ While | Until } condition ]
  • 7. Looping Through Code Using Do…Loop Statement
    • You can exit a Do...Loop by using the Exit Do statement.
    • Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the Exit Do statement in the True statement block of an If ... Then ... Else statement.
    • If the condition is False , the loop runs as usual.
    Dani Vainstein
  • 8. Looping Through Code Using Do…Loop Statement
    • Dim bCheck, Count
    • bCheck = True : iCount = 0 ' Initialize variables.
    • Do ' Outer loop.
    • Do While iCount < 20 ' Inner loop .
    • iCount = iCount + 1 ' Increment Counter.
    • If iCount = 10 Then ' If condition is True...
    • bCheck = False ' set value of flag to False.
    • Exit Do ' Exit inner loop.
    • End If
    • Loop
    • Loop Until bCheck = False ' Exit outer loop immediately.
    Dani Vainstein
  • 9. Looping Through Code Using While…Wend Statement
    • Executes a series of statements as long as a given condition is True .
    • The While ... Wend statement is provided in VBScript for those who are familiar with its usage.
    • However, because of the lack of flexibility in While ... Wend , it is recommended that you use Do ... Loop instead.
    • The While ... Wend statement is provided in VBScript for those who are familiar with its usage.
    Dani Vainstein
  • 10. Looping Through Code Using For…Next Statement
    • Repeats a group of statements a specified number of times.
    • You can use For ... Next statements to run a block of statements a specific number of times.
    • For loops, use a counter variable whose value increases or decreases with each repetition of the loop
    Dani Vainstein
      • Dim x
      • For x = 1 To 50
      • MyProc
      • Next
  • 11. Looping Through Code Using For…Next Statement
    • Using the Step keyword, you can increase or decrease the counter variable by the value you specify.
    • In the following example, the counter variable j is incremented by 2 each time the loop repeats.
    • When the loop is finished, the total is the sum of 2, 4, 6, 8, and 10.
    Dani Vainstein
      • Dim j, itotal
      • For j = 2 To 10 Step 2
      • itotal = itotal + j
      • Next
      • Msg = &quot;The total is &quot; & itotal
  • 12. Looping Through Code Using For…Next Statement
    • You can exit any For...Next statement before the counter reaches its end value by using the Exit For statement.
    • Because you usually want to exit only in certain situations, such as when an error occurs, you should use the Exit For statement in the True statement block of an If...Then...Else statement.
    • If the condition is False , the loop runs as usual.
    Dani Vainstein
  • 13. Looping Through Code Using For Each...Next Statement
    • A For Each...Next loop is similar to a For...Next loop.
    • Instead of repeating the statements a specified number of times, a For Each...Next loop repeats a group of statements for each item in a collection of objects or for each element of an array.
    • This is especially helpful if you don't know how many elements are in a collection.
    Dani Vainstein
  • 14. Looping Through Code Exit Statement
    • Exits a block of Do...Loop , For...Next , Function , or Sub code.
      • Exit Do
        • Provides a way to exit a Do...Loop statement.
        • It can be used only inside a Do...Loop statement.
      • Exit For
        • Provides a way to exit a For loop.
        • It can be used only in a For...Next or For Each...Next loop.
      • Exit Function
        • Immediately exits the Function procedure in which it appears.
      • Exit Property
        • Immediately exits the Property procedure in which it appears.
      • Exit Sub
        • Immediately exits the Sub procedure in which it appears.
    Dani Vainstein
  • 15. Excercises Dani Vainstein
  • 16. Lab 5.1
    • Input parameters names are From and To
    • Assume that both inputs are integers.
    • Display in reporter all the pairs between For and To
    • Use the For...Next loop.
    • Tip
      • Use the Step statement.
      • Concatenate to a temporary string.
    • Try your program for the following values
      • From = 2 : To = 10 (2,4,6,8,10)
      • From = 1 : To = 9 (2,4,6,8)
      • From = -7 : To = 3 (-6,-4,-2,0,2)
      • From = 50 : To = 41 (50,48,46,44,42)
      • From = -3 : To = -9 (-4,-6,-8)
    Dani Vainstein
  • 17. Lab 5.2
    • Input parameters names are Pswd.
    • Declare a constant MY_PASSWORD.
    • Declare a constant MAX_RETRY = 3.
    • If the password is correct display a micPass message.
    • If the password is typed wrong more then MAX_RETRY times, display a micFail message.
    • Use the Do..Loop ( While or Until ) .
    • Tip
      • Use the Exit Loop statement.
    Dani Vainstein
  • 18. Make sure to visit us
    • Tutorials
    • Articles
    • Projects
    • And much more
    • www.AdvancedQTP.com