Iteration (Looping Constructs in VB) Iteration: Groups of … PPT/XI… · PPT file · Web...
Transcript of Iteration (Looping Constructs in VB) Iteration: Groups of … PPT/XI… · PPT file · Web...
Iteration (Looping Constructs in VB)Iteration: Groups of statements which are repeatedly
executed until a certain test is satisfied
Carrying out Iteration in VB programming, requires the use of Looping Constructs
There 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]
Determinate LoopsA group of statement is repeated a specific number of times
For..Next Loops
Repeat the statements in a loop a specific number of timesEach For statement has a corresponding Next statement
For..Next Loop syntax:
For Counter/LoopIndex = Start To End [step step]Statements (Body of Loop)
Next Counter/LoopIndex
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 executed
Start and End may be Constants, Variables, Numeric Property Values, or Numeric Expressions, and further
determine the initial and final value of the counter
The 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
Dim iLoopIndex As IntegerDim iMaximum As Integer
iMaximum = 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 loop
Next iLoopIndex
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 loop
For iIndex = 2 To 100 Step 2
will 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
Exiting For..Next Loops
If you enter an Endless Loop, the program execution will have to be broken manually
Therefore, you will need to enter Break Time Ctrl + Break
With For..Next loops, you may need to terminate the loop before the loop index reaches its final value
VB provides an Exit For statement for this situation
Generally, an Exit For statement is part of an If statement
For iLoopIndex = 1 To 10 If txtInput.Text = “ ” Then ‘nothing was entered into the input textbox
MsgBox “You must enter something” Exit For
End If……….………. ‘statements in the loop
Next iLoopIndex
Do LoopsA Do..Loop terminates based on a condition that is
specified
Execution of a Do..Loop continues while a condition is True or until a condition is True
The condition can be placed at the Top or at the Bottom of the loop
Align the Do and Loop statements with each other and indent the lines of code to be repeated in the body of the
loop
Checking the condition before entering the loop:
Do {While / Until} Condition‘statements in the loop
LoopTesting 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
Checking the condition after one iteration of the loop:
Do ‘statements in the loop
Loop {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
Private Sub cmdCalculate_Click( )'Given an Account Balance and an Interet Rate, it returns the number'of years it will take to reach $10,000
'Declare the VariablesDim iNumYears As IntegerDim cAccountBal As CurrencyConst sngInterestRate As Single = 0.1
'The Account Balance is taken from the user through an inputboxcAccountBal = InputBox("Enter the Balance in the Account which you hold", "AccountBalance")
'Assign iNumYears a value of 0 (which is the counter for the Loop)iNumYears = 0
'Loop runs until cAccountBal exceeds $10,000Do Until cAccountBal > 10000 'Increases cAccountBal by sngInterestRate cAccountBal = cAccountBal + (cAccountBal * sngInterestRate) 'iNumYears increased by 1 with each execution of the loop iNumYears = iNumYears + 1LooptxtNumYears.Text = ("The number of years it will take is " & iNumYears)
End Sub
Private Sub cmdNesting_Click( )'Gets input from the user and prints a 5 times table from'that input
'Set the counter variablesDim OuterCount As IntegerDim InnerCount As Integer
'Set up the number to be inputted by the userDim FirstNum As Integer
OuterCount = 1FirstNum = InputBox("What is the Value of the First Number", "First Number")
'InnerLoop runs 5 times for every time the OuterLoop runs once
Do While OuterCount <= FirstNum
'This is the beginning of the Inner Loop For InnerCount = 1 To 5 Print InnerCount * OuterCount Next InnerCount 'This is the end of the Inner Loop
Print 'This prints a blank line
OuterCount = OuterCount + 1Loop
End Sub
Private Sub cmdLoop5Times_Click( )'Checks 5 times whether the user's name is of a certain length'(in this case 10 characters in length).'After the 5th iteration of the loop it ends.
'Declare the user input variable and the varible counterDim UserName As String 'Accepts user inputDim NumLoops As Integer 'Used as the loop counter
'Set the value of the counter NumLoopsNumLoops = 0
'Loop runs until the length of UserName is > 10 characters'in length, or the user has entered their name 5 times or more
Do 'UserName gets a value from an inputbox UserName = InputBox("Please Enter Your Name", "Enter Name") txtName.Text = UserName 'Adds 1 to the counter variable NumLoops NumLoops = NumLoops + 1 If (Len(UserName) < 10) Then txtName.Text = "" MsgBox "Please enter a UserName greater than 10 characters in length",vbinformationonly, "User Name Length" End IfLoop Until (Len(UserName) > 10) Or (NumLoops >= 5)
End Sub
Private Sub cmdExitDo_Click( )'Checks 5 times whether the user's name is of a certain length'(in this case 10 characters in length).'After the 5th iteration of the loop it ends by using the'Exit Do statement
'Declare the user input variable and the varible counterDim UserName As String 'Accepts user inputDim NumLoops As Integer 'Used as the loop counter
'Set the value of the counter NumLoopsNumLoops = 0
'Loop runs until the length of UserName is > 10 characters'in length, or the user has entered their name 5 times or more
Do 'UserName gets a value from an inputbox UserName = InputBox("Please Enter Your Name", "Enter Name") txtName.Text = UserName 'Adds 1 to the counter variable NumLoops NumLoops = NumLoops + 1 'If the number of iterations of the loop is greater than 'or equal to 5 then the loop is exited, through the use 'of the Exit Do statement If NumLoops = 5 Then txtName.Text = "" MsgBox "You have been given 5 chances to enter a name of the correct lengthand failed to do so", vbCritical, "User Name Length Information" End If If NumLoops >= 5 Then Exit Do
If (Len(UserName) < 10) Then txtName.Text = "" MsgBox "Please enter a UserName greater than 10 characters in length",vbinformationonly, "User Name Length" End IfLoop Until (Len(UserName) > 10)
End Sub
Example
Code 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
Example
Code 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.