1 / 17

Chapter 6 – Repetition

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.

taro
Download Presentation

Chapter 6 – Repetition

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops

  2. Overview • Pre-Test Loops Do While Condition … Loop • Post-Test Loops Do …Loop Until Condition • Counters • Accumulators • Sentinel Values

  3. 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.

  4. 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.

  5. Pseudocodeand Flow Chart

  6. 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?

  7. 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

  8. 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.

  9. How many years does it take to become a millionare when investing money at 6%? How Long To Make A Million $ txtAmount txtWhen

  10. 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

  11. The Output

  12. 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.

  13. 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.

  14. 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"

  15. 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!

  16. Pseudocodeand Flowchart

  17. 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.

More Related