1 / 16

Chapter 6 – Repetition

Chapter 6 – Repetition. 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops. 6.2 For…Next Loops. General Form of a For…Next Loop Nested For…Next Loops Local Type Inference. For…Next Loops. Used when we know how many times we want the loop to execute A counter controlled loop.

sabine
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. 6.2 For…Next Loops • General Form of a For…Next Loop • Nested For…Next Loops • Local Type Inference

  3. For…Next Loops • Used when we know how many times we want the loop to execute • A counter controlled loop

  4. For…Next Loop Syntax

  5. Simple For … Next Loop For i As Integer = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next The loop counter variable, i, is • initialized to 1 • tested against the stop value, 5 • 6 when the loop terminates … then dies! • incremented by 1 at the Next statement

  6. Comparing a While and For Loop Dim i As Integer = 1 Do While i <= 5 lstTable.Items.Add(i & " " & i ^ 2) i += 1 Loop COMPARED TO: For i As Integer = 1 To 5 lstTable.Items.Add(i & " " & i ^ 2) Next

  7. Population: Output Display a table showing the population of a city of 300,000 people that is expected to grow by 3% each year from 2010 to 2014.

  8. Population: Code Dim pop As Double = 300000 For yr As Integer = 2010 To 2014 lstTable.Items.Add(yr & " " & FormatNumber(pop, 0)) pop += 0.03 * pop Next yr could go from 1 to 5, however it increases the readability of the problem by using 2010 to 2014. After each pass, how much does the counter variable yr increment by?

  9. Step Clause • Normally after each pass the value of the counter variable increases by 1 • If the clause Step s is appended to the For statement, the value of s will be added to the counter variable after each pass. For i As Integer = 2 To 10 Step 2 lstTable.Items.Add(i & "" & i^2) Next Adds 2 to i at the end of each pass.

  10. Example with a Negative Step Value For j As Integer = 10 To 1 Step -1 lstBox.Items.Add(j) Next lstBox.Items.Add("Blastoff") To execute the statements within the loop, the initial value will be greater than the final value If the step value is a negative number, the value of the counter variable will decrease after each pass.

  11. Example: Nested For…Next Loops For i As Integer = 65 To 70 For j As Integer = 1 To 3 lstBox.Items.Add(Chr(i) & j) Next Next Output: A1 A2 A3 B1 B2 B3 : Outer loop Inner loop Remeber Chr of 65 is “A”

  12. For and Next Pairs • For and Next statements must be paired. • If one is missing, the automatic syntax checker will complain with a wavy underline and a message such as “A ‘For’ must be paired with a ‘Next’.”

  13. Start, Stop, and Step values • Consider a loop beginning with For i As Integer = m To n Step s • The loop will be executed exactly once if m equals n no matter what value s has. • The loop will not be executed at all if m is greater than n and s is positive, or if m is less than n and s is negative.

  14. Altering the Counter Variable • The value of the counter variable should not be altered within the body of the loop. • Doing so might cause the loop to repeat indefinitely or have an unpredictable number of repetitions.

  15. Non-Integer Step Values • Can lead to round-off errors with the result that the loop is not executed the intended number of times. • We will most often use Integers for all values in the For header. For j As Double= 1.0 To 5.0 Step .1 lstBox.Items.Add(j) Next

  16. Local Type Inference • Often there is no need to have a variable exist after the for loop is complete. For j As Integer = 1 To 5 lstBox.Items.Add(j) Next lstBox.Items.Add(j) j is out of scope after the for loop

More Related