1 / 21

CS105 Lab 9 – Do-While-Loops

CS105 Lab 9 – Do-While-Loops. Announcements MP2 released today! See website for several important announcements/deadlines. Objectives. Develop a more sophisticated paint program Nested-For-Loop Do-Until-Loop Generate a random number. What we will do. Last week, we: Painted

nadine
Download Presentation

CS105 Lab 9 – Do-While-Loops

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. CS 105 – Fall 2009 CS105 Lab 9 – Do-While-Loops • Announcements • MP2 released today! • See website for several important announcements/deadlines

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

  3. CS 105 – Fall 2009 What we will do • 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. CS 105 – Fall 2009 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? For intOffset = 0 To mintBrushSize – 1 Cells(intRow + intOffset, intCol).Interior.Color = vbBlue Next intOffset “+intOffset” ? “+intOffset” ? For intOffset = 0 To mintBrushSize – 1 Cells(intRow, intCol + intOffset).Interior.Color = vbBlue Next intOffset

  5. CS 105 – Fall 2009 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 Nested-If • 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

  6. CS 105 – Fall 2009 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 • 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. CS 105 – Fall 2009 Paint a square, with the ActiveCell in the center • The previous code painted a square to • the right and bottom of the ActiveCell. How can we draw it aroundtheActiveCell? For intRowOffset = -2 To 2 For intColOffset = -2 To 2 Cells(intRow + intRowOffset, intCol + _ intColOffset).Interior.Color = vbBlue Next intColOffset Next intRowOffset

  8. CS 105 – Fall 2009 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

  9. CS 105 – Fall 2009 Do-Until Loop Flowchart Start Is loop Condition true? Execute statements in loop body No Yes End

  10. CS 105 – Fall 2009 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

  11. CS 105 – Fall 2009 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 ‘NOTE: intRow and intCol hold the position of ‘the current cell intRow = ActiveCell.Row intCol = ActiveCell.Column intColOffset = 0

  12. CS 105 – Fall 2009 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

  13. CS 105 – Fall 2009 Brush until hitting blue cells. • We want to paint lines of mintBrushSize size untilthey 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

  14. CS 105 – Fall 2009 Paint until hitting a blue cell. ‘NOTE: intRow and intCol hold the position of ‘the current cell intRow = ActiveCell.Row 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

  15. CS 105 – Fall 2009 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?

  16. CS 105 – Fall 2009 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 0 1 Rnd() Scaling up 0 56 Color Index 1 2 …. 55 56

  17. CS 105 – Fall 2009 Generate a random number (cont) • What if we want a random number between 2 and 10? • Answer : Int( Rnd() * (10-2+1) ) + 2 0 1 Rnd() Scaling up 0 9 Color Index 2 3 …. 10 11

  18. CS 105 – Fall 2009 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

  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?

  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.

  21. Exercises

More Related