Vb scripting

2,314 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
2,314
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
110
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Vb scripting

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

×