1 / 16

CS105 Lab 12 – Do Loops

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.

Télécharger la présentation

CS105 Lab 12 – Do 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. 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

  2. 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

  3. 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

  4. 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

  5. “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

  6. “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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

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

  13. 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

  14. 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

  15. 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

  16. 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

More Related