Download presentation
Presentation is loading. Please wait.
1
Do … Loop Until (condition is true)
3.3 Iteration Loops Do … Loop Until (condition is true) 12/01/2019
2
Learning Objectives State when the Do … Loop Until (condition is true) should be used. State the general form of the Do … Loop Until (condition is true). State the minimum number of times the Do … Loop Until (condition is true) will be executed and why. State when Do … Loop Until (condition is true) will be exited. Use Boolean variables. 12/01/2019
3
The code above will produce:
Dim Number As Integer For Number = 5 To 9 Console.WriteLine(Number*Number) Next Number The code above will produce: 25 36 49 64 81
4
Do … Loop Until (condition is true)
Is a “post-condition” loop.
5
Differences Between “For …. To …. Next” & “Do …. Loop …
Differences Between “For …. To …. Next” & “Do …. Loop …. Until” Iteration Loops
6
Manually Incrementing a Variable:
Number = Number + 1 Or Number += 1 Both will increment a variable by one from the previous value. 12/01/2019
7
Console.WriteLine(Number*Number) Next Number To 9
Note that as “Do … Loop Until” loops have their end point at the end of the loop (instead of at the beginning, as they are in “For …To … Next” loops) the end point has to be raised to ensure that the loop ends the same way. Dim Number As Integer Number = 5 Do Console.WriteLine(Number*Number) Number = Number + 1 Loop Until Number = 10 Dim Number As Integer For Number = 5 Console.WriteLine(Number*Number) Next Number To 9 Start End Number is automatically incremented in a “For …To … Next” loop but not in a “Do … Loop Until” loop.
8
Start / End Point Issue Note that the line to increment:
Number = Number + 1 Could be placed at the beginning of a “Do … Loop Until” loop. This would avoid the end point having to be raised but then the start condition would have to be lowered: Dim Number As Integer Number = 4 Do Console.WriteLine(Number*Number) Loop Until Number = 9
9
Disadvantages of “Do …. Loop …. Until” Iteration Loops
Complexity: Start point needs to be manually set before the loop starts: Number = 5 Do Incrementing is automatic, so extra manual incrementing line needed: Number = Number + 1 Start / End point issue: Start point needs to be lowered or the End point needs to be raised. Depending on whether you place the manual incrementing line above after the beginning or before the end of the loop.
10
Advantages of “Do …. Loop …. Until” Iteration Loops
More flexibility to incorporate strings (text) conditions in the start and end points of the loop. Dim Number As Integer Dim UserStop As String Number = 5 Do Console.WriteLine(Number*Number) Console.WriteLine("Do you want to stop? Enter Yes or No.") UserStop = Console.ReadLine Number = Number + 1 Loop Until Number = 10 or UserStop = “Yes”
11
Strings (text) end points in “For …. To …. Next” iteration loops
The only way to do this in a “For …. To …. Next” loop is with an If condition inside the loop and exit the loop if the string end point is met. Dim Number As Integer Dim UserStop As String For Number = 5 To 9 Console.WriteLine(Number*Number) Console.WriteLine("Do you want to stop? Enter Yes or No.") UserStop = Console.ReadLine If UserStop = “Yes” Then Exit For ‘Exit the loop if the user wants to stop. End If Next Number
12
Do … Loop Until (condition is true)
If you omitted the line: Number = Number + 1 The loop would never end as Number would never equal 10. This is called an infinite loop. If you find your program in an infinite loop then you will have to switch to VB and click the Debug menu and click the Stop Debugging option. 12/01/2019
13
General Form of a Do … Loop Until (condition is true)
Loop body statements Loop Until (condition is true) Since the condition is set at the end of the loop the loop body must be executed at least once. The loop will be exited when the condition becomes true. 12/01/2019
14
Program 3.3a Password Entry
Specification: Allow the user up to three attempts at entering a password. Inform the user which attempt they are currently on. Inform the user that the password is correct. Otherwise inform them that their password is invalid and then asks them if they wish to continue. 12/01/2019
15
Program 3.3a Password Entry
Create a new project named ‘Password Entry’. 12/01/2019
16
Program 3.3a Password Entry
‘Declare variables. Dim Password As String = "Secret“ ‘Set Password. Dim PasswordAttempt As String Dim Attempt As Integer Dim Cancel As Boolean = False 12/01/2019 Continued on next slide.
17
Program 3.3a Password Entry
Do ‘Start of loop. Attempt = Attempt + 1 ‘Increase Attempt by 1. ‘Ask user for password. Console.WriteLine("Enter Password. " & “Attempt: " & Attempt) PasswordAttempt = Console.ReadLine If PasswordAttempt = Password Then ‘Is password valid? Console.WriteLine("This password is valid.") ‘Password valid. Else ‘Password invalid. Console.WriteLine("This password is invalid.") ‘Inform user of invalid password. Console.WriteLine(“Do you wish to stop/cancel? (True/False)”) Cancel = Console.ReadLine End If ‘End of If statement. ‘Exit if password is valid or if user has had 3 attempts. Loop Until PasswordAttempt = Password Or Attempt = 3 Or Cancel = True 12/01/2019 Continued on next slide.
18
Program 3.3a Password Entry
‘Has the user had 3 attempts with an invalid password? If Attempt = 3 And PasswordAttempt <> Password Then ‘If so inform the user that they have had 3 attempts. Console.WriteLine("You have had 3 attempts.Your account is blocked") End If ‘End of If statement. 12/01/2019
19
Program 3.3a Password Entry
Run the program and test it. 12/01/2019
20
Infinite Loops If we removed the line
Attempt = Attempt + 1 If the user does not enter the correct password then the loop would never end as the condition Attempt = 3 would never come true. 12/01/2019
21
Given pseudocode will use the following structure:
REPEAT <statement(s)> UNTIL <condition>
22
“Commenting Out” When you are asked change previous code from previous programs it is helpful and even necessary for to keep the original code to be changed visible, without VB executing it. This can be done by “commenting out” the original code so that VB treats the lines as comments (ignores them) but you as a human can still see and read them.
23
Extension 3.3b Change any of the “For …. To …. Next” loop programs you wrote in 3.1 Loops or 3.2 Loops to use a: Do … Loop Until (condition is true) instead. With the incrementing line at the end of the loop, immediately before Loop Until …. e.g. Do … Index = Index + 1 Loop Until Index = ….. Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Do … Loop Until version).
24
Extension 3.3c Change any of the “For …. To …. Next” loop programs you wrote in 3.1 Loops or 3.2 Loops to use a: Do … Loop Until (condition is true) instead. With the incrementing line at the beginning of the loop, immediately after Do e.g. Do Index = Index + 1 … Loop Until Index = ….. Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (Do … Loop Until version).
25
Extension 3.3d Change this Program 3.3 Password Entry program to use a
“For …. To …. Next” Loop Hints: The “Do Loop …. Until” Loop’s end condition of PasswordAttempt = Password cannot be used as part of the “For …. To …. Next” Loop’s end condition Only the end condition Attempt = 3. As “For …. To …. Next” Loops count and cannot work with strings. So you will need Exit For to get out of the loop after the password has been entered correctly, this means after the line: MsgBox("This password is valid.") Make a copy of the previous program’s whole folder to keep the original program but rename this folder with the same name but add (For …. To …. Next Version). 12/01/2019
26
Plenary When should the Do … Loop Until (condition is true) be used?
Used if you do not know exactly how many times the loop should be repeated. What is the general form of the Do … Loop Until (condition is true)? Do Loop body statements Loop Until (condition is true) 12/01/2019
27
Plenary What is the minimum number of times the Do … Loop Until (condition is true) will be executed and why? Once, since the condition is set at the end of the loop When will Do … Loop Until (condition is true) be exited? The loop will be exited when the condition becomes true. 12/01/2019
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.