1 / 40

Iteration

Iteration. Conditional Loops Counted Loops. Declare vars for input and result. get input. produce result from input. show result. Charting the Flow of Control. We’ve used flow charts to visualise the flow of control. The simplest form is sequence . False. True. Condition. Process.

mandel
Download Presentation

Iteration

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. Iteration Conditional Loops Counted Loops

  2. Declare vars for input and result get input produce result from input show result Charting the Flow of Control • We’ve used flow charts to visualise the flow of control. • The simplest form is sequence.

  3. False True Condition Process Process Charting the Flow of Control • We’ve also used flow charts to map several different styles of selection. • eg.If/Then/Else

  4. Flow of Control • The third way control can be transferred is called iteration, repetition,or looping. • There are several ways to describe a looping process. For example: • Repeat the following steps if conditions are right. • conditional loop • Repeat the following steps X times. • counted loop

  5. Repeat the following steps if conditions are right. • “if conditions are right” can mean different things: • … until there’s no more data. • … while there’s still data. • The difference is simply in how the condition is stated. • Using “the following steps” implies that the condition will be tested before the steps are executed. • This is called a pre-test.

  6. N stop? Y process data step1 step2… increment counter Pre-test Do Until Do Until counter = max step1 step2 step3 counter = counter + 1 Loop

  7. Pre-test Do While Do While counter < max step1 step2 step3 counter = counter + 1 Loop Y repeat? N process data step1 step2… increment counter

  8. Post-test Loops • There are other ways of expressing conditional loops: • Repeat steps 1, 2, 3, & 4 until there’s no more data. • Repeat steps 1, 2, 3, & 4 while there’s still data. • These are the same types of conditions. • The difference is that the condition will be tested after the steps are executed each time. • This is called a post-test.

  9. process data step1 step2… increment counter N stop? Y Post-test Do Until Do step1 step2 step3 counter = counter + 1 Loop Until counter = max

  10. process data step1 step2… increment counter Y repeat? N Post-test Do While Do step1 step2 step3 counter = counter + 1 Loop While counter < max

  11. Repeat the following steps X times. Counted loops are so common that most programming languages have a special syntax which optimises counted loops. In VB6 this structure is implemented as the For/Next loop. For loopIndex = start To finish ‘ body of loop Next loopIndex

  12. initialise loop index variable N limit exceeded? Y loop body increment loop index For/Next Loops Dim counter As Integer For counter = 1 To 5 ‘ loop body Next counter For/Next loops use a pre-test, and have a Do Until style.

  13. For/Next Loops In the most general case, For/Next loops use 4 integer parameters. Dim counter As Integer ‘loop index Dim start As Integer ‘initial value Dim finish As Integer ‘final value Dim increment As Integer ‘delta value Forcounter=startTofinishStepincrement ‘ loop body Next counter

  14. For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ Forcounter= -11 To 2 Step 4 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________

  15. For/Next Loops Since they are integers they can have positive or negative values. _________________________________________________________ Forcounter= -11 To 2 Step 4 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________ counter will have the values -11, -7, -3, 1.

  16. For/Next Loops Another example. _________________________________________________________ Forcounter= 10 To 0 Step -3 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________

  17. For/Next Loops Another example. _________________________________________________________ Forcounter= 10 To 0 Step -3 ‘ loop body Nextcounter _________________________________________________________ What values counter will have? _________________________________________________________ counter will have the values 10, 7, 4, 1.

  18. For/Next Loops The start, finish, and increment values can be specified as literals or as Integer variables declared before the loop. The loopindex must be an Integer variable. It can be declared before the loop – or not! The For statement can act as its declaration.

  19. For/Next Loops The scope of counter extends beyond the loop so its value can be used or changed when the loop is finished. This may be a useful side effect. Dim counter As Integer For counter = 1 To 5 ‘loop body Next counter txtDisplay.Text = “Counter is ” & Str(Counter)

  20. For/Next Loops Dim counter As Integer Dim start As Integer Dim finish As Integer Dim increment As Integer For counter = 1 To 5 start = -2 finish = 10 increment = 5 Next counter VB ignores attempts to change the loop parameters within the loop, except…

  21. For/Next Loops …the loop index. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter Simulate this program to see what values will be displayed. skip

  22. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  23. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  24. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  25. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  26. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  27. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  28. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  29. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  30. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  31. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  32. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter For/Next Simulation

  33. Dim counter As Integer For counter = 1 To 10 ‘display Counter counter = counter + 3 Next counter In general this is poor programming. For/Next Simulation

  34. For/Next Loops Here’s an even poorer example. Dim counter As Integer For counter = 1 To 5 counter = 1 Next counter Since counter is set to 1 each time the loop executes it can never reach 5, so the loop is infinite. Avoid changing the loop index!

  35. Nested Loops The body of a loop can contain any VB elements, including a loop. For outerIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & Str(innerIndex)) Next innerIndex Next outerIndex What is the ‘output’ from this loop?

  36. Notes VB allows programmers to omit the loop index from the Next statement: For counter = start To finish ‘ loop body Nextcounter

  37. Notes This can make code difficult to follow. For outerIndex = 0 To 9 For middleIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & _ (Str(middleIndex ) & Str(innerIndex)) Next Next Next

  38. Notes • Proper use of indentation also helps. For outerIndex = 0 To 9 For middleIndex = 0 To 9 For innerIndex = 0 To 9 ‘display (Str(outerIndex) & _ (Str(middleIndex ) & Str(innerIndex)) Next innerIndex Next middleIndex Next outerIndex

  39. Notes VB allows programs to exit For - Next loops before termination is reached. For counter = start To finish Exit For Next

  40. Notes Usually this command is controlled by an If statement. For counter = start To finish If <some other condition> Then Exit For End If Next

More Related