1 / 31

Iteration in VB Programming

Learn about iteration in VB programming and how to use looping constructs to repeat groups of statements until a certain test is satisfied. Explore determinate and indeterminate loops, along with examples and tips for exiting loops.

betsye
Download Presentation

Iteration in VB Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iteration (Looping Constructs in VB)Iteration: Groups of statements which are repeatedly executed until a certain test is satisfiedCarrying out Iteration in VB programming, requires the use of Looping ConstructsThere are 2 main types of Looping Constructs:Determinate Loops: will repeat themselves a known (specific) number of times (For..Next Loops)Indeterminate Loops: will repeat themselves an unknown number of times (Do Loops) [Until; While]

  2. Determinate LoopsA group of statement is repeated a specific number of timesFor..Next LoopsRepeat the statements in a loop a specific number of timesEach For statement has a corresponding Next statementFor..Next Loop syntax: For Counter/LoopIndex = StartTo End[step step]Statements (Body of Loop) Next Counter/LoopIndex

  3. The For..Next loop uses the For and Next statements and a Counter Variable [LoopIndex]The elements of a For..Next:The Counter/LoopIndex must be a numeric variable,determines the number of times the statements inside the loop will be executedStart and Endmay be Constants, Variables, Numeric Property Values, or Numeric Expressions, and further determine the initial and final value of the counterThe optional word Step may be included, along with the value to be added to the LoopIndex (positive/negative) for each iteration of the loop, and if omitted, the default value is 1 for each increment of the loop

  4. Dim iLoopIndex As IntegerDim iMaximum As IntegeriMaximum = Inputbox(“Enter the value”, Number of Entries)For iLoopIndex = 0 To iMaximum‘The statements inside of the loop are indented, and referred to as ‘the body of the loopNext iLoopIndex

  5. A Counter-Controlled Loop generally has 3 elements:Initialise the CounterIncrement the Counter [step]Test the Counter to determine when it is time to Terminate the loopFor iIndex = 2 To 100 Step 2will count from 2 to 100 by 2The statements in the body of the loop will be executed 50 times, with iIndex = 2, 4, 6, ….The program checks for greater than the test value and not equal to

  6. Exiting For..Next LoopsIf you enter an Endless Loop, the program execution will have to be broken manuallyTherefore, you will need to enter Break TimeCtrl + BreakWith For..Next loops, you may need to terminate the loop before the loop index reaches its final valueVB provides an Exit For statement for this situationGenerally, an Exit For statement is part of an If statement

  7. For iLoopIndex = 1 To 10If txtInput.Text = “ ” Then‘nothing was entered into the input textbox MsgBox “You must enter something”Exit ForEnd If ………. ………. ‘statements in the loopNext iLoopIndex

  8. Do LoopsA Do..Loop terminates based on a condition that is specifiedExecution of a Do..Loop continues while a condition is True or until a condition is TrueThe condition can be placed at the Top or at the Bottom of the loopAlign the Do and Loop statements with each other and indent the lines of code to be repeated in the body of the loop

  9. Checking the condition before entering the loop:Do {While / Until} Condition‘statements in the loopLoop Testing for completion at the top of the loop Also known as a pretest The statements inside the loop may never be executed if the terminating condition is True, the first time it is tested iTotal = 0 Do Until iTotal = 0 ‘statements in the loop Loop

  10. Checking the condition after one iteration of the loop:Do ‘statements in the loopLoop {While / Until} Condition Testing for completion at the bottom of the loop Also known as a posttest The statements inside the loop will always be executed at least once iTotal = 0 Do ‘statements in the loop Loop Until iTotal = 0

  11. ExampleCode a procedure that prompts the user for a password to login. If the password is not greater then 5 characters then a message is displayed to the user. The user is allowed 3 attempts to login, if unsuccessful at this point the application shuts down

  12. ExampleCode a procedure that checks 5 times whether the username is of a certain length (in this case greater then 10 characters). After the fifth attempt the user is locked out of the application.

More Related