Download presentation
Presentation is loading. Please wait.
Published byTrevor Oliver Modified over 9 years ago
1
30/10/20151 3.4 Iteration Loops Do While (condition is true) … Loop
2
230/10/2015 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.
3
330/10/2015 Dim Number As Integer Number = 5 Do Console.WriteLine(Number*Number) Console.WriteLine(Number*Number) Number = Number + 1 Number = Number + 1 Loop Until Number = 10 The code above will produce: 25 25 36 36 49 49 64 64 81 81
4
Do While (condition is true) … Loop Is a “pre-condition” loop.
5
530/10/2015 Differences Between “Do …. Loop …. Until” & “Do While …. Loop” Iteration Loops
6
630/10/2015 Dim Number As Integer Number = 5 DoConsole.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).
7
730/10/2015 Dim Number As Integer Number = 5 Do While Number <= 9 lstNumber.Items.Add(Number*Number) Number = Number + 1 Loop Dim Number As Integer For Number = 5 To 9 lstNumber.Items.Add(Number*Number) Next Number Start Number is automatically incremented in a “For …To … Next” loop but not in a “Do While …. Loop” loop. Differences Between “For …. To …. Next” & “Do While …. Loop” Loops
8
830/10/2015 Start / End Point Issue Note that the line to increment: Number = Number + 1 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 Number = Number + 1 Number = Number + 1 Console.WriteLine(Number*Number) Console.WriteLine(Number*Number)Loop
9
930/10/2015 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. i.e. it will be exited when the Boolean condition in the first line becomes false.
10
1030/10/2015 General Form of a Do While (condition is true) … Loop Do While (condition is true) Loop body statements Loop body statementsLoop 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).
11
1130/10/2015 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. Can be used to set an initial value of a variable when it is declared.
12
1230/10/2015 Program 3.4a Password Entry 2 Specification: Same as 3.3a Password Entry but: Same as 3.3a Password Entry but:3.3a Password Entry3.3a Password Entry Use Do While … Loop.
13
Replace the lines beginning and ending the loop with: ‘Allow the user to enter a password as long as they haven’t ‘Allow the user to enter a password as long as they haven’t ‘already had 3 attempts. ‘already had 3 attempts. Do While Attempt Password And Cancel <> False Do While Attempt Password And Cancel <> False……..……..…….. Loop ‘Signifies the end of the loop. Loop ‘Signifies the end of the loop. Go to next slide. 1330/10/2015 Program 3.4a Password Entry 2
14
1430/10/2015 Program 3.4a Password Entry 2 You will now notice a green squiggly line underneath PasswordAttempt in the line: Do While Attempt Password Do While Attempt 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: Change the line: Dim PasswordAttempt As String to: to: To set a value to the PassWordAttempt variable so the variable is not tested before it has a value Dim PasswordAttempt As String = “ “ ‘.
15
1530/10/2015 Program 3.4a Password Entry 2 Run the program and test it.
16
1630/10/2015 Extension 3.4a Password Entry 2 Extend the program so that: If Attempt = 1 the program outputs “First try is wrong. Please try again”. 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 second try the program outputs “Password still wrong. One more chance”. On the third try the program outputs “No valid password entered”. On the third try the program outputs “No valid password entered”.
17
Given pseudocode will use the following structure: WHILE ENDWHILE
18
1830/10/2015 Extension 3.4b Change one of the “For …. To …. Next” loop programs you have written in 3.1 or 3.2 Loops to use a: 3.13.23.13.2 Do While (condition is true) … Loop Do While (condition is true) … Loopinstead. With the incrementing line at the end of the loop, immediately before Loop e.g. Do While Index …. Do While Index ….……… Index = Index + 1 Loop 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).
19
1930/10/2015 Extension 3.4c Change one of the “For …. To …. Next” loop programs you have written in 3.1 or 3.2 Loops to use a: 3.13.23.13.2 Do While (condition is true) … Loop Do While (condition is true) … Loopinstead. With the incrementing line at the beginning of the loop, immediately after Do …. e.g. Do While Index …. Do While Index …. Index = Index + 1 ……… Loop 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).
20
2030/10/2015 Extension 3.4d Change the “Do … Loop Until (condition is true)” loop program you changed in 3.3 Loops to use a: 3.3 Loops3.3 Loops Do While (condition is true) … Loop Do While (condition is true) … Loopinstead. You changed it from a “For …. To …. Next” initially into a “Do … Loop Until (condition is true)” in 3.3 Loops. 3.3 Loops3.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).
21
2130/10/2015 Extension 3.4e Change this Program 5.4 Password Entry 2 program to use a “For …. To …. Next” Loop “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).
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
2330/10/2015 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. To reset you will need to set all variables to their initial states / default values. See the next slide for help with this.
24
2430/10/2015 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) 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) repeatedly ask for input until the input is 0 (zero) keep a count of each choice keep a count of each choice on completion of data entry, print out the results as a tally chart (as shown above) on completion of data entry, print out the results as a tally chart (as shown above) Write this program.
25
Variables – Initial States / Default values When variables are declared they are set up with an “initial state” / “default value”: 25 Data TypesInitial State / Default Value Byte / Integer / Decimal0 String / Char“” (blank / empty / null) BooleanFalse Date / TimeNothing
26
2630/10/2015 Round function Used for rounding numbers and currency. Needs two arguments. Variable or constant to be formatted. Variable or constant to be formatted. The number of decimal places to be rounded to. The number of decimal places to be rounded to. Needs to be proceeded in VB by Math. Needs to be proceeded in VB by Math. General Form: Math.Round( VariableOrNumberToBeRounded, NumberOfDecimalPlacesToBeRoundedTo ) Extension “Average” Program 3.4g Extension “Average” Program 3.4g
27
2730/10/2015 Random function Gives a random number between 0 and 1. To generate whole numbers between 2 specified values: Randomize() ' initialize the random-number generator. 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. Int(Rnd() * HigherValue ) + LowerValue Int(Rnd() * HigherValue ) + LowerValuee.g. ' To generate random value between 1 and 6. ' To generate random value between 1 and 6. Int((Rnd())*6) + 1 Extension “Average” Program 3.4g Extension “Average” Program 3.4g
28
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)”. 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. Stops the user entering marks less than 0 and more than 100. Then displays: The highest and lowest marks. The highest and lowest marks. See next 2 slides for some hints for this. The average mark rounded to 2 decimal places. The average mark rounded to 2 decimal places. See slide 25. 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. See slide 26.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). 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).
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” button 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” button 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 28.35 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: 31
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. 32
33
33 30/10/2015 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. 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? 0 What is the general form of the Do While (condition is true) … Loop? Do While (condition is true) Do While (condition is true) Loop body statements Loop Loop
34
3430/10/2015 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. 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. It will be exited when the Boolean condition in the first line becomes false.
35
3530/10/2015 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. 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. 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.
36
3630/10/2015 Plenary How do we set an initial value of a variable when it is declared. Dim … As … = … Dim … As … = … Can be used to set an initial value of a variable when it is declared.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.