170 likes | 338 Vues
Chapter 6 – Repetition. 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops. Overview. Pre-Test Loops Do While Condition … Loop Post-Test Loops Do … Loop Until Condition Counters Accumulators Sentinel Values. 6.1 Do Loops.
E N D
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops
Overview • Pre-Test Loops Do While Condition … Loop • Post-Test Loops Do …Loop Until Condition • Counters • Accumulators • Sentinel Values
6.1 Do Loops • A loop is one of the most important structures in programming. • Used to repeat a sequence of statements a number of times. • The Do loop repeats a sequence of statements either as long as or until a certain condition is true.
Pre-Test Do Loop Condition is tested, If it is true, the loop is run. If it is false, the statements following the Loop statement are executed. Do Whilecondition statement(s) Loop These statements are inside the body of the loop and are run if the condition above is true.
Pre-Test Do While Private Sub btnDisplay_Click(...)Handles btnDisplay.Click Dim num As Integer = 1 'Initialize the counter Do While num <= 5 'Test the counter lstNumbers.Items.Add(num) num += 1 'Add 1 to the value of num Loop End Sub 'Display the numbers from 1 to 5 with a counter How would you modify the above code to make it print out values from 5 to 100 by 5s?
Sentinel-Controlled Loop DimnumAs Double = 0 Dim prompt As String prompt = “Enter a nonnegative #. (-1 to Quit” num = CDbl(InputBox(prompt)) 'TOP Do Whilenum <> -1 '-1 is sentinel value . .. num = CDbl(InputBox(prompt)) 'Bottom Loop
Accumulating a Sum Private Sub Button1_Click…) Handles Button1.Click Dim num As Double = 0, sum As Double = 0, ave As DoubleDim count As Double = 0 Dim prompt As String = "Enter a number. (-1 to Quit)" num = CDbl(InputBox(prompt)) Do While num <> -1 '-1 is sentinel value count = count + 1 sum = sum + num num = CDbl(InputBox(prompt)) Loop ave = sum / count ListBox1.Items.Add("Average = " & ave) End Sub Write code to find the average of any number of numbers.
How many years does it take to become a millionare when investing money at 6%? How Long To Make A Million $ txtAmount txtWhen
The Code Private Sub btnCalculate_Click(...) Handles _ btnCalculate.Click Dim balance As Double, numYears As Integer balance = CDbl(txtAmount.Text) Do While balance < 1000000 balance += 0.06 * balance numYears += 1 Loop txtWhen.Text = "In " & numYears & " years you will have a million dollars." End Sub
Password Program Dim passWord As String = "" Do While passWord <> "SHAZAM" passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop passWord is the loop control variable because the value stored in passWord is what is tested to determine if the loop should continue or stop.
Post-Test Do…Until Loop Do statement(s) Loop Untilcondition Loop is executed once and then the condition is tested. If it is false, the loop is run again. If it is true, the statements following the Loop statement are executed.
Converting Between Pre-Test and Post-Test Loop Conditions • Converting between pre-test and post-test loops requires negating the condition. • = <> And Or • < >= • > <= Do WhilepassWord <> "SHAZAM" converts to: Loop UntilpassWord = "SHAZAM"
Repeat Request Until Proper Response Dim passWord As String = "" Do passWord = InputBox("What is the password?") passWord = passWord.ToUpper Loop Until passWord = "SHAZAM" Post-test loops are okay to use when you are sure the statements in the loop will be executed at least once!
Comments • Avoid infinite loops – loops that never end. • Infinite loops occur when it is not possible to reach a loop condition Do While num <> -1 count = count + 1 sum = sum + num Loop • Visual Basic allows for the use of either the While keyword or the Until keyword at the top or the bottom of a loop.