160 likes | 473 Views
CS105 Lab 12 – Do Loops. Announcements MP5 is available and is due on Tuesday, November 13 th at 10:00pm . Quiz 5 is now available in Mallard and is due on Tuesday, November 13 th at 10:00pm . Midterm 2 feedback sheets will be handed out today in lab. Objectives.
E N D
CS105 Lab 12 – Do Loops • Announcements • MP5 is available and is due on Tuesday, November 13th at 10:00pm. • Quiz 5 is now available in Mallard and is due on Tuesday, November 13th at 10:00pm. • Midterm 2 feedback sheets will be handed out today in lab. CS 105 – Fall 2007
Objectives • Learn about the two kinds of Do loops • Do While • Do Until • Download the worksheet for Lab 12 at http://www.cs.uiuc.edu/class/cs105 CS 105 – Fall 2007
What we will do • Today in the lab: • We have a field bounded by the wall • User selects a cell and put a house in there • Then user selects another cell and let aimless guy start from it • Aimless guy is wondering aimlessly until he reaches the house • Keyword here is UNTIL CS 105 – Fall 2007
Guy’s and House’s location • Two module-level variables: • mintCurrentRow holds guy’s row numeric value • mintCurrentColumn holds guy’s column value • Another two module-level variables: • mintHouseRow holds house’s row numeric value • mintHouseColumn holds house’s column value CS 105 – Fall 2007
“Set House” button • Provided code for “Set House” button does the following • Assigns Active Cell coordinates to the House’s location coordinates • Clears the field • Put the house icon at the house’s coordinates • Reset step counter • Enables “Start” button CS 105 – Fall 2007
“Start” button • Write code for “Start” button that does the following • Declares intMoves variable and initializes it to 0 • Assigns Active Cell coordinates to the Guy’s location coordinates • Moves the guy until he is in the House • Disables “Start” button after finished CS 105 – Fall 2007
Moving the guy • Move the guy Until he reaches the house • Use Do Until loop with two conditions: • Current Row is equal to house’s Row • mintCurrentRow = mintHouseRow • Current Column is equal to house’s Column • mintCurrentColumn = mintHouseColumn • What logical operator will we use here? CS 105 – Fall 2007
How do we move the guy? • In your loop do the following: • Call DrawMan subprocedure • Call MakeOneStep subprocedure • Increase counter intMoves • Update counter on a spread sheet • Counter must be displayed in the cell P10 CS 105 – Fall 2007
For your information • Where do we use Do Loops: • When we do not know exactly how many times we have to do something, but we know when to stop • Here, we do not know how many steps it will take for a guy to reach the house, but we are sure that he need to take another step if he is not in there • We must ensure condition to be true eventually, otherwise loop will never stop • Use Ctrl-Break or Esc to get out of an infinite loop CS 105 – Fall 2007
Do-Until Loop Flowchart Start Is loop condition true? No (false) Execute statements in loop • 1st, check to see if loop should start • If so, execute code • Then test again • And so on… Yes (true) End CS 105 – Fall 2007
Syntax of a Do Loops • Do-Until has the following syntax Do Until <condition> <…code to execute…> Loop • Do-While has the following syntax Do While <condition> <…code to execute…> Loop CS 105 – Fall 2007
Where do we need while loop? • Now, run your program • We have a supersonic aimless guy!!! • We need to calm him down, and to do that we will write a Delay sub-procedure CS 105 – Fall 2007
Delay sub-procedure • Write Delay sub-procedure and do the following in it: • Declare sngStartTime variable • Take current time using Timer function • Wait WHILE current time is less then sngStartTime + msngDelayTime Timer Timer Timer Timer sngStartTime = Timer sngStartTime + msngDelayTime CS 105 – Fall 2007
Difference between Do While & Do Until • Do While executes while the condition is true. • Do Until executes until the condition is true. • You can replace one with the other by simply negating your condition with Not operator CS 105 – Fall 2007
MakeMove sub-procedure • Check MakeMove sub-procedure • What you notice different from what we had? • The condition can also be at the bottom of the loop: • VBA checks the condition after it executes the loop body • Note: this kind of loop always executes at least once CS 105 – Fall 2007
Do Loop Until Flowchart Start • Execute a code • Check to see if loop should finish • If not do previous steps • And so on… Execute statements in loop Is loop condition true? No (false) Yes (true) End CS 105 – Fall 2007