cs105 lab 9 – do-while-loops announcements mp2 released today! see website for several important...

21
CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 2009 1

Upload: marilyn-mason

Post on 13-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

CS105 Lab 9 – Do-While-Loops

Announcements MP2 released today!

See website for several important announcements/deadlines

CS 105 – Fall 2009 1

Page 2: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Objectives

Develop a more sophisticated paint program

Nested-For-Loop Do-Until-Loop Generate a random number

CS 105 – Fall 2009 2

Page 3: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

What we will do

CS 105 – Fall 2009 3

Last week, we: Painted

Horizontally/Right Vertically/Down Diagonally/Right

Changed the Brush Size

This week, we will do Paint Up and Left Paint a Square Paint a line until it

hits something Paint with random

color and length

Page 4: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

For intOffset = 0 To mintBrushSize – 1Cells(intRow + intOffset, intCol).Interior.Color = vbBlue

Next intOffset

Last week

REMEMBER: We used a For-Loop to paint a line

How do we draw a line downward?

How do we draw a line rightward?

CS 105 – Fall 2009 4

For intOffset = 0 To mintBrushSize – 1Cells(intRow, intCol + intOffset).Interior.Color = vbBlue

Next intOffset

“+intOffset” ?“+intOffset” ?

Page 5: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

REMEMBER: What we call an IF inside an IF? In a similar way, we call a For-Loop inside a

For-Loop a Nested-For-Loop

Paint a square

To paint a 5x5 square, we need to draw 5 lines, each containing 5 cells

We need a For-Loop to paint lines 1 through 5: AND for each line, we need another

For-Loop to draw 5 cells

CS 105 – Fall 2009 5

Nested-If

Page 6: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Paint a square

For intRowOffset = 0 To 4 For intColOffset = 0 To 4 Cells(intRow + intRowOffset, intCol + _

intColOffset).Interior.Color = vbBlue Next intColOffsetNext intRowOffset

CS 105 – Fall 2009 6

Dim intRowOffset As IntegerDim intColOffset As Integer

Tip: to observe the nested For-Loop stepby step you can call the “Delay” sub here

Page 7: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Paint a square, with the ActiveCell in the center

For intRowOffset = -2 To 2For intColOffset = -2 To 2

Cells(intRow + intRowOffset, intCol + _ intColOffset).Interior.Color = vbBlue

Next intColOffsetNext intRowOffset

CS 105 – Fall 2009 7

The previous code painted a square to the right and bottom of the ActiveCell. How can we draw it around the ActiveCell?

Page 8: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Paint until it hits a blue cell.

We want to paint a line from thethe ActiveCell, until it hits anothercell that is painted blue.

How do we do this? We don’t know how many cells we have to paint.

Therefore we cannot use a For-Loop. But we do know “when to stop” . We STOP when the cell

we are painting is already blue

We should use a: Do-Until-LoopCS 105 – Fall 2009 8

Page 9: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

CS 105 – Fall 20099

Start

Is loopCondition

true?

End

Yes

Execute statements in loop body

No

Do-Until Loop Flowchart

Page 10: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

CS 105 – Fall 2009 10

Syntax of a Do-Until Loops

Do-Until has the following syntax:Do Until <condition>

<…code to execute…>Loop

Pseudo-code:Do Until “the current cell is blue”

“paint the current cell blue” “move current cell one step to the

right”Loop

Page 11: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Paint until hitting a blue cell.

Do Until Cells(intRow,intCol+intColOffset).Interior.Color = vbBlue Cells(intRow, intCol+intColOffset).Interior.Color = vbBlue intColOffset = intColOffset + 1Loop

CS 105 – Fall 2009 11

‘NOTE: intRow and intCol hold the position of

‘the current cell

intRow = ActiveCell.RowintCol = ActiveCell.ColumnintColOffset = 0

Page 12: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Infinite Loop

When does the Do-Until-Loop finish? ONLY when the loop condition is TRUE What happens if the loop condition is never TRUE?

NOTE: The loop condition has to eventually be TRUE otherwise the loop will NEVER stop, becoming an infinite loop.

Use Ctrl-Break or Esc to terminate an infinite loop

CS 105 – Fall 2009 12

Page 13: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Brush until hitting blue cells.

We want to paint lines of mintBrushSize size until they we come across blue cells

How do we do this? We know how to draw one line, using a Do-Until-Loop We know how to do this mintBrushSize times, using a For-

Loop

We should use a: Do-Until-Loop nested inside a For-Loop

CS 105 – Fall 2009 13

Page 14: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Paint until hitting a blue cell.

For intOffset = 0 to mintBrushSize -1 intCol = ActiveCell.ColumnDo Until Cells(intRow +intOffset, intCol).Interior.Color =

vbBlue Cells(intRow+intOffset, intCol).Interior.Color = vbBlue intCol = intCol + 1Loop

Next intOffset

CS 105 – Fall 2009 14

‘NOTE: intRow and intCol hold the position of

‘the current cell

intRow = ActiveCell.Row

Page 15: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Finally, make a masterpiece !

We want to paint with a random color

VBA has 56 color index, ranging from1 to 56.

REMEMBER: To paint a particular cell with a color index we wrote:

….Interior.ColorIndex=[color_index]

How do we get a random color_index between 1 and 56? CS 105 – Fall 2009 15

Page 16: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Generate a random number

VBA provides a built-in function called Rnd() that gives a random floating-point number in [0,1).

How can we convert the random number provided by Rnd() into an integer between 1 and 56?

Answer : Int( Rnd() * 56 ) + 1 CS 105 – Fall 2009 16

0 1

Rnd()

Scaling up

Color Index

1 2 …. 55 56

0 56

Page 17: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Generate a random number (cont)

What if we want a random number between 2 and 10?

Answer : Int( Rnd() * (10-2+1) ) + 2

CS 105 – Fall 2009 17

0 1

Rnd()

Scaling up

Color Index

2 3 …. 10 11

0 9

Page 18: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Finally, make a masterpiece !

Private Sub cmdPaintRandom_Click()Dim intRow As IntegerDim intCol As IntegerintRow = ActiveCell.RowintCol = ActiveCell.Column 'TODO : call Randomize sub procedure

Randomize'Two variables to hold a random color index and random length

Dim intRandomLength As IntegerDim intRandomColor As IntegerDim intOffset As Integer'TODO : get a random integer between 1 and 56 and assign to intRandomColor

intRandomColor = Int(Rnd() * 56) + 1'TODO : get a random integer between 1 and 10 and assign to intRandomLength

intRandomLength = Int(Rnd() * (10-2+1)) + 2

'This is the incrementing variable for the For-loop'for loop to paint "mintBrushSize" cells at a time'TODO : use intRandomLength here

For intOffset = 0 To intRandomLength – 1'TODO : use intRandomColor hereCells(intRow + intOffset, intCol).Interior.ColorIndex =

intRandomColorNext intOffset

End Sub procedure to make it more random

CS 105 – Fall 2009 18

Page 19: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

What you should know?

How to write a Nested-For-Loop? How to write a Do-Until-Loop

How to nest it inside a For-Loop How to generate a random integer

number?

19

Page 20: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Exercises

Add more “Paint until Hit” buttons, each does a different direction : up, down, left, right

Do the same for “Paint Randomly”

Do the above, but this time, paint in 4 diagonal directions : North-East, South-East, South-West, North-West.

20

Page 21: CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091

Exercises

21

Do-Loop-While Do-Loop-Until Do-Until-LoopSub Test1 (intA As Integer) Do intA = intA + 1 Loop While intA < 10End Sub

Sub Test2 (intA As Integer)

Do intA = intA + 1 Loop Until intA < 10End Sub

Sub Test3 (intA As Integer)

Do Until intA >= 10 intA = intA + 1 LoopEnd Sub

Exercise

# Times

value of intA?

Exercise#

Timesvalue of

intA?Exercise

# Times

value of intA?

Test1(1) Test2(10) Test3(100)

Test2(1) Test3(10) Test1(0)

Test3(1) Test1(100)

Test1(-10)

Test1(10)

Test2(100)

Test2(9)