for loops and nested loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... ·...
TRANSCRIPT
![Page 1: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/1.jpg)
Chapter 4
A Guide to Working with Visual Logic 1
For Loops and Nested Loops
![Page 2: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/2.jpg)
Use Loops – to draw pictures
A Guide to Working with Visual Logic 2
![Page 3: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/3.jpg)
Review – Loops with Counters/LCV
A Guide to Working with Visual Logic 3
� 3 Steps in Loops with Loop Control Variables � Initialize LCV before the loop starts � Test Condition using LCV � Update LCV in body of the loop
![Page 4: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/4.jpg)
While Loop (increment by 1)
A Guide to Working with Visual Logic 4
![Page 5: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/5.jpg)
While - For Loop (increment by 1)
A Guide to Working with Visual Logic 5
![Page 6: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/6.jpg)
While Loop - increment more than 1
A Guide to Working with Visual Logic 6
![Page 7: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/7.jpg)
While - For Loop - increment more than 1
A Guide to Working with Visual Logic 7
![Page 8: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/8.jpg)
For Loop - increment more than 1
A Guide to Working with Visual Logic 8
![Page 9: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/9.jpg)
For Loops
A Guide to Working with Visual Logic 9
� For loops are used to automate the initialize, test and update process
� In Visual Logic, the For loop flowchart element is a six-sided figure with a loop variable, a start (initial) value, an end (final) value, an update (step) value and two arrows (one loops back, the other exits out of the loop)
![Page 10: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/10.jpg)
For Loops – Positive Step
A Guide to Working with Visual Logic 10
� When executed, the first action in the loop is to initialize the loop variable to the start value
� If the step is positive: the body of the loop executes as long as the value of the loop variable does not exceed the final value. (i.e. loop variable <= final value)
� After the body of the loop executes, the loop variable is updated by the step value and the process is repeated
� Can the step be negative: ???
![Page 11: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/11.jpg)
While Loop - Decrement
A Guide to Working with Visual Logic 11
![Page 12: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/12.jpg)
While – For Loop - Decrement
A Guide to Working with Visual Logic 12
![Page 13: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/13.jpg)
For Loop - Decrement
A Guide to Working with Visual Logic 13
![Page 14: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/14.jpg)
For Loops – Negative Step
A Guide to Working with Visual Logic 14
� If the step is negative, the body of the loop executes as long as the value of the loop variable is greater than or equal to the final value.
� After the body of the loop executes, the loop variable is updated by the step value and the process is repeated.
![Page 15: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/15.jpg)
Upcoming: Use Loops – to draw pictures
A Guide to Working with Visual Logic 15
![Page 16: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/16.jpg)
A Guide to Working with Visual Logic 16
![Page 17: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/17.jpg)
Review – For Loop
A Guide to Working with Visual Logic 17
What is the Initial Value? What is the Final Value? What is the Step?
Output:
![Page 18: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/18.jpg)
Counting: Initial, Final and Step as inputs
A Guide to Working with Visual Logic 18
The initial, final and step values in a for loop can be variables
![Page 19: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/19.jpg)
Nested Loops
A Guide to Working with Visual Logic 19
� First we will look at several examples and understand Nested Loops
� Later we will learn to recognize a program that needs
Nested Loops and how to write the nested loops
![Page 20: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/20.jpg)
I Love Counting with for loops! (demo)
A Guide to Working with Visual Logic 20
![Page 21: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/21.jpg)
Nested Loop – Example 1
A Guide to Working with Visual Logic 21
While Loop
For Loop
![Page 22: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/22.jpg)
Nested Loop – Example 1 - Output
A Guide to Working with Visual Logic 22
![Page 23: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/23.jpg)
Nested Loop
A Guide to Working with Visual Logic 23
� Any valid statements can occur inside the body of a loop, including input, assignment, output, conditions and even other loops
� A nested loop refers to a loop that appears inside the body of another loop
![Page 24: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/24.jpg)
Nested Loop Example – (Figure 4-9 (a))
A Guide to Working with Visual Logic 24
While Loop For Loop
![Page 25: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/25.jpg)
Fig: 4-9 (b)
A Guide to Working with Visual Logic 25
While Loop
For Loop
![Page 26: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/26.jpg)
“Hello .. Hello “ (Figure 4 – 11 a, b)
A Guide to Working with Visual Logic 26
Begin
End
Count1 to 16
Output:"Hello"
Begin
End
Count1 to 5
K1 to 3
Output:"Hello"
Output:"Hello"
![Page 27: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/27.jpg)
“Hello” ??? times (Figure 4 – 11 c)
A Guide to Working with Visual Logic 27
Begin
End
Count1 to 4
K1 to 3
Output:"Hello"
Output:"Hello"
![Page 28: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/28.jpg)
Multiplication Table
A Guide to Working with Visual Logic 28
![Page 29: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/29.jpg)
Using Nested Loops to draw pictures
A Guide to Working with Visual Logic 29
![Page 30: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/30.jpg)
Nested Loops – Triangle Problem
A Guide to Working with Visual Logic 30
![Page 31: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/31.jpg)
Nested Loops
A Guide to Working with Visual Logic 31
• Is there repetition in this output? • Many lines • Within each line there are many stars • How many lines? • Lines: 1 .. 10 • Each line has many stars. • Line 1 has 1 star… • Line 2 has 2 stars…. • Line 10 has 10 stars • For each LineCount from 1 to 10 • Output Many stars – How
many ?
![Page 32: For Loops and Nested Loops - dbrodersen.comdbrodersen.com/wordpress/wp-content/uploads/2012/... · For Loops and Nested Loops . Use Loops – to draw pictures 2 A Guide to Working](https://reader031.vdocuments.mx/reader031/viewer/2022021416/5a81760d7f8b9a38478d4776/html5/thumbnails/32.jpg)
Pseudo-code – Triangle Problem
A Guide to Working with Visual Logic 32
For LineCount from 1 to 10 do For StarCount from 1 to LineCount do
Output “*” EndFor Output (NewLine)
EndFor