32
Chapter 4 A Guide to Working with Visual Logic 1 For Loops and Nested Loops

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

  • Upload
    ngonhu

  • View
    296

  • Download
    1

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

“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

“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

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

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

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

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

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