Download presentation
Presentation is loading. Please wait.
1
Do While (condition is true) … Loop
3.4 Iteration Loops Do While (condition is true) … Loop 28/12/2018
2
Learning Objectives State when the Do While (condition is true) … Loop should be used. State the general form of the Do While (condition is true) … Loop. State when Do While (condition is true) … Loop will be executed. State when Do While (condition is true) … Loop will be exited. State and explain the difference between Do While (condition is true) … Loop and Do … Loop Until (condition is true). Explain what the Round and Random functions do and how to use them. Setting an initial value of a variable when it is declared. Explain how to “reset” by returning variables to their initial states / default values. 28/12/2018
3
The code above will produce:
Dim Number As Integer Number = 5 Do Console.WriteLine(Number*Number) Number = Number + 1 Loop Until Number = 10 The code above will produce: 25 36 49 64 81 28/12/2018
4
Do While (condition is true) … Loop
Is a “pre-condition” loop.
5
Differences Between “Do …. Loop …. Until” & “Do While …
Differences Between “Do …. Loop …. Until” & “Do While …. Loop” Iteration Loops 28/12/2018
6
Console.WriteLine(Number*Number) Number = Number + 1
Dim Number As Integer Number = 5 Do Console.WriteLine(Number*Number) Number = Number + 1 Loop Until Number = 10 Dim Number As Integer Number = 5 Do While Number <= 9 Console.WriteLine(Number*Number) Number = Number + 1 Loop The end is now simply Loop. 1. The focus is now on when to start or continue rather than when to end. 2. So the end point is reversed into a when to start or continue condition and is placed at the beginning of the loop rather than at the end. 3. Note that this means that the “start/continue” point is lowered (comparable to the end point of a “For …. To …. Next” loop). 28/12/2018
7
Differences Between “For …. To …. Next” & “Do While …. Loop” Loops
Dim Number As Integer For Number = 5 To 9 lstNumber.Items.Add(Number*Number) Next Number Dim Number As Integer Number = 5 Do While Number <= 9 lstNumber.Items.Add(Number*Number) Number = Number + 1 Loop Start Number is automatically incremented in a “For …To … Next” loop but not in a “Do While …. Loop” loop. 28/12/2018
8
Start / End Point Issue Note that the line to increment:
Number = Number + 1 Could be placed at the beginning of a “Do While … Loop” loop. This would lower the “when to start or continue” condition and lower the “start” value. Dim Number As Integer Number = 4 Do While Number <= 8 Console.WriteLine(Number*Number) Loop 28/12/2018
9
Do While (condition is true) … Loop
Used if there is a possibility that the loop body statements should not be executed. Runs as long as the boolean condition in the first line of the loop is true, otherwise it exits. i.e. it will be exited when the Boolean condition in the first line becomes false. 28/12/2018
10
General Form of a Do While (condition is true) … Loop
Loop body statements Loop Since the condition is set at the beginning of the loop the loop body may not be executed at all i.e. skipped. This feature distinguishes it from the Do … Loop Until (condition is true). 28/12/2018
11
Setting an initial value of a variable when it is declared.
Dim … As … = … Can be used to set an initial value of a variable when it is declared. 28/12/2018 11
12
Program 3.4a Password Entry 2
Specification: Same as 3.3a Password Entry but: Use Do While … Loop. 28/12/2018
13
Program 3.4a Password Entry 2
Replace the lines beginning and ending the loop with: ‘Allow the user to enter a password as long as they haven’t ‘already had 3 attempts. Do While Attempt < 3 And PasswordAttempt <> Password And Cancel = False …….. Loop ‘Signifies the end of the loop. Go to next slide. 28/12/2018
14
Program 3.4a Password Entry 2
You will now notice a green squiggly line underneath PasswordAttempt in the line: Do While Attempt < 3 And PasswordAttempt <> Password Hover over and read the error message that pops up. VB is warning you that you are testing the value of the AttemptPassword variable before it has been set a value. Actually this is just a warning and will not usually cause problems. However, it is good programming practice to solve it by setting a value when the variable is declared: Change the line: Dim PasswordAttempt As String to: ‘To set a value to the PassWordAttempt variable so the variable is not tested before it has a value Dim PasswordAttempt As String = “ “ 28/12/2018
15
Program 3.4a Password Entry 2
Run the program and test it. 28/12/2018
16
Extension 3.4a Password Entry 2
Extend the program so that: If Attempt = 1 the program outputs “First try is wrong. Please try again”. On the second try the program outputs “Password still wrong. One more chance”. On the third try the program outputs “No valid password entered”. 28/12/2018
17
Given pseudocode will use the following structure:
WHILE <condition> <statement(s)> ENDWHILE
18
Extension 3.4b Do While (condition is true) … Loop
Change one of the “For …. To …. Next” loop programs you have written in 3.1 or 3.2 Loops to use a: Do While (condition is true) … Loop instead. With the incrementing line at the end of the loop, immediately before Loop e.g. Do While Index …. … Index = Index + 1 Loop 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 While … Loop Version). 28/12/2018
19
Extension 3.4c Do While (condition is true) … Loop
Change one of the “For …. To …. Next” loop programs you have written in 3.1 or 3.2 Loops to use a: Do While (condition is true) … Loop instead. With the incrementing line at the beginning of the loop, immediately after Do …. e.g. Do While Index …. Index = Index + 1 … Loop 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 While … Loop Version). 28/12/2018
20
Extension 3.4d Do While (condition is true) … Loop
Change the “Do … Loop Until (condition is true)” loop program you changed in 3.3 Loops to use a: Do While (condition is true) … Loop instead. You changed it from a “For …. To …. Next” initially into a “Do … Loop Until (condition is true)” in 3.3 Loops. 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 While … Loop Version). 28/12/2018
21
Extension 3.4e Change this Program 3.4 Password Entry 2 program to use a “For …. To …. Next” Loop 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). 28/12/2018
22
Extension Programs 3.4 f - j
Please feel free to use any of the 3 forms of loops you deem appropriate in the following extension programs. However, I do suggest you attempt to balance your use each of the 3 forms, so that you are confident in their use. For … To … Next Do … Loop Until Do While … Loop
23
Extension “Running Total” Program 3.4f
Write a program to allow a user to enter a series of numbers and see their running total as each is entered. After each number is entered the user should be asked if he/she wishes to “Continue”, “Stop” or “Reset” (start again from 0). To reset you will need to set all variables to their initial states / default values. See the next slide for help with this. 28/12/2018 23
24
Extension “Sports Tally” Program 3.4g
Rema surveys the students in her class to find out which is the most popular sport. She draws a tally chart: Rema plans to collect sport data from students in the whole school. She designs a program to: input the number of the sport a student likes best (1, 2, 3 or 4) repeatedly ask for input until the input is 0 (zero) keep a count of each choice on completion of data entry, print out the results as a tally chart (as shown above) Write this program. 28/12/2018 24
25
Variables – Initial States / Default values
When variables are declared they are set up with an “initial state” / “default value”: Data Types Initial State / Default Value Byte / Integer / Decimal String / Char “” (blank / empty / null) Boolean False Date / Time Nothing
26
Round function Used for rounding numbers and currency.
Needs two arguments. Variable or constant to be formatted. The number of decimal places to be rounded to. Needs to be proceeded in VB by Math. General Form: Math.Round(VariableOrNumberToBeRounded, NumberOfDecimalPlacesToBeRoundedTo) 28/12/2018 26 Extension “Average” Program 3.4g 26
27
Random function Gives a random number between 0 and 1.
To generate whole numbers between 2 specified values: Either use: Randomize() ' initialize the random-number generator. ‘Note that without initialising, only one random will be generated per program run; so the same random number will be used each time the Rnd() function is called, during that run. RandomNumber = Int(Rnd() * HigherValue ) + LowerValue ‘ To generate random value between 1 and 6. e.g. Int((Rnd())*6) + 1 Or: Dim Rnd as New Random RandomNumber = Rnd .Next(HigherValue , LowerValue) e.g. ' To generate random value between 1 and 6. RandomNumber = RandomNumber.Next(6, 1) 28/12/2018 27 Extension “Average” Program 3.4g 27
28
Extension “Average” Program 3.4h
Allow the user to enter as many exam marks as they wish. After each mark is entered ask the user “Do you wish to calculate the Average now? (True / False – means enter more numbers)”. Use a Boolean variable. Stops the user entering marks less than 0 and more than 100. Then displays: The highest and lowest marks. See next 2 slides for some hints for this. The average mark rounded to 2 decimal places. See slide 25. Then asks the user if they wish to “Exit” or “Reset” (and start again). Allow the user to use automatically generated random marks. See slide 26. Ask the user at the beginning whether they wish to enter marks or use randomly generated ones (if they do wish random numbers then also ask how many). 28
29
Average – Highest & Lowest
Possible Solution 1: As the marks have to be from 0 to 100 we can use these borderline values as the initial highest and lowest values respectively by setting: Dim Lowest As Integer = 100 Dim Highest As Integer = 0 Make sure you include an explanation of these lines in your comments. Each time a mark is entered, compare the newly entered mark with the variable Lowest (NewMark<Lowest). If it is lower then change the Lowest mark. If NewMark < Lowest Then Lowest = NewMark Do the same for Highest but in reverse. Remember to display the Lowest and Highest in appropriate labels. When you comment on your If statements make sure you explain where you are testing and why does it have to be here, what you are testing for, why are you testing for this and what happens if the test is true. Remember to edit the “Reset” code as well. A second possible solution is on the next slide, I will leave you to decide which one you choose to actually use..
30
Average – Highest & Lowest
Possible Solution 2: The first mark entered (butOK) could be stored in both the Lowest & Highest variables. If NumberOfMarks = 1 Then Lowest = Highest = NewMark You will then need to either position this new code at a certain place in the existing code or move the line which updates the NumberOfMarks (NumberOfMarks = NumberOfMarks + 1) to before you test for the first mark above or change how you test for the first mark. Each time a mark is entered, compare the newly entered mark with the variable Lowest (NewMark<Lowest). If it is lower then change the Lowest mark. If NewMark < Lowest Then Lowest = NewMark Do the same for Highest but in reverse. Remember to display the Lowest and Highest in appropriate labels. When you comment on your If statements make sure you explain where you are testing and why does it have to be here, what you are testing for, why are you testing for this and what happens if the test is true. Remember to edit the “Reset” code as well. I will leave you to decide which solution you choose to actually use.
31
Extension “Conversion Ounces to Grams” Program 3.4i
Sheena has inherited a recipe book from her grandmother. All the recipes give ingredient measurements in ounces. Sheena wants to write a program to produce a conversion table that helps her use the correct weight in grams. To convert ounces into grams: 1 ounce is grams. The conversion table will show the number of grams to the nearest whole number: The built-in function ROUND(x) returns x rounded to the nearest whole number. Write a program to print the conversion table for 1 to 16 ounces:
32
Extension “Number Guessing Game” Program 3.4j
Raza wants to write a number-guessing game. He has drawn a flowchart of an algorithm: Write this program.
33
Plenary When should the Do While (condition is true) … Loop be used?
Used if there is a possibility that the loop body statements should not be executed. So what is the minimum number of times a Do While (condition is true) … Loop may be executed? What is the general form of the Do While (condition is true) … Loop? Do While (condition is true) Loop body statements Loop 28/12/2018
34
Plenary When will Do While (condition is true) … Loop be executed?
Runs as long as the Boolean condition in the first line of the loop is true, otherwise it exits. When will Do While (condition is true) … Loop be exited? It will be exited when the Boolean condition in the first line becomes false. 28/12/2018
35
Plenary State and explain the difference between Do While (condition is true) … Loop and Do … Loop Until (condition is true). The difference is that the loop body statements of a Do While (condition is true) … Loop may not be executed at all but a Do … Loop Until (condition is true) has to be executed at least once. This is because in a Do While (condition is true) … Loop the condition is at the beginning whereas in a Do … Loop Until (condition is true) the condition is at the end. 28/12/2018
36
Plenary How do we set an initial value of a variable when it is declared. Dim … As … = … Can be used to set an initial value of a variable when it is declared. 28/12/2018
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.