Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "CS105 Lab 9 – Do-While-Loops Announcements MP2 released today! See website for several important announcements/deadlines CS 105 – Fall 20091."— Presentation transcript:

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

2 Objectives Develop a more sophisticated paint program Nested-For-Loop Do-Until-Loop Generate a random number CS 105 – Fall 20092

3 What we will do CS 105 – Fall 20093 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

4 For intOffset = 0 To mintBrushSize – 1 Cells(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 20094 For intOffset = 0 To mintBrushSize – 1 Cells(intRow, intCol + intOffset).Interior.Color = vbBlue Next intOffset “+intOffset” ?

5 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 20095 Nested-If

6 Paint a square For intRowOffset = 0 To 4 For intColOffset = 0 To 4 Cells(intRow + intRowOffset, intCol + _ intColOffset).Interior.Color = vbBlue Next intColOffset Next intRowOffset CS 105 – Fall 20096 Dim intRowOffset As Integer Dim intColOffset As Integer Tip: to observe the nested For-Loop step by step you can call the “Delay” sub here

7 Paint a square, with the ActiveCell in the center For intRowOffset = -2 To 2 For intColOffset = -2 To 2 Cells(intRow + intRowOffset, intCol + _ intColOffset).Interior.Color = vbBlue Next intColOffset Next intRowOffset CS 105 – Fall 20097  The previous code painted a square to the right and bottom of the ActiveCell. How can we draw it around the ActiveCell?

8 Paint until it hits a blue cell. We want to paint a line from the the ActiveCell, until it hits another cell 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-Loop CS 105 – Fall 20098

9 9 Start Is loop Condition true? End Yes Execute statements in loop body No Do-Until Loop Flowchart

10 CS 105 – Fall 200910 Syntax of a Do-Until Loops Do-Until has the following syntax: Do Until Loop Pseudo-code: Do Until “the current cell is blue” “paint the current cell blue” “move current cell one step to the right” Loop

11 Paint until hitting a blue cell. Do Until Cells(intRow,intCol+intColOffset).Interior.Color = vbBlue Cells(intRow, intCol+intColOffset).Interior.Color = vbBlue intColOffset = intColOffset + 1 Loop CS 105 – Fall 200911 ‘NOTE: intRow and intCol hold the position of ‘the current cell intRow = ActiveCell.Row intCol = ActiveCell.Column intColOffset = 0

12 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 200912

13 Brush until hitting blue cells. We want to paint lines of mintBrushSize size until theywe 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 200913

14 Paint until hitting a blue cell. For intOffset = 0 to mintBrushSize -1 intCol = ActiveCell.Column Do Until Cells(intRow + intOffset, intCol).Interior.Color = vbBlue Cells(intRow +intOffset, intCol).Interior.Color = vbBlue intCol = intCol + 1 Loop Next intOffset CS 105 – Fall 200914 ‘NOTE: intRow and intCol hold the position of ‘the current cell intRow = ActiveCell.Row

15 Finally, make a masterpiece ! We want to paint with a random color VBA has 56 color index, ranging from 1 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 200915

16 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 200916 01 Rnd() Scaling up Color Index 12….5556 0

17 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 200917 01 Rnd() Scaling up Color Index 23….1011 09

18 Finally, make a masterpiece ! Private Sub cmdPaintRandom_Click() Dim intRow As Integer Dim intCol As Integer intRow = ActiveCell.Row intCol = ActiveCell.Column 'TODO : call Randomize sub procedure Randomize 'Two variables to hold a random color index and random length Dim intRandomLength As Integer Dim intRandomColor As Integer Dim 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 here Cells(intRow + intOffset, intCol).Interior.ColorIndex = intRandomColor Next intOffset End Sub procedure to make it more random CS 105 – Fall 200918

19 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

20 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

21 Exercises 21 Do-Loop-WhileDo-Loop-UntilDo-Until-Loop Sub Test1 (intA As Integer) Do intA = intA + 1 Loop While intA < 10 End Sub Sub Test2 (intA As Integer) Do intA = intA + 1 Loop Until intA < 10 End Sub Sub Test3 (intA As Integer) Do Until intA >= 10 intA = intA + 1 Loop End Sub Exercise # Timesvalue of intA?Exercise# Timesvalue of intA?Exercise# Timesvalue 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)


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

Similar presentations


Ads by Google