Ppt lesson 10

1,508 views

Published on

Published in: Technology, Health & Medicine
1 Comment
0 Likes
Statistics
Notes
  • Be the first to like this

No Downloads
Views
Total views
1,508
On SlideShare
0
From Embeds
0
Number of Embeds
168
Actions
Shares
0
Downloads
27
Comments
1
Likes
0
Embeds 0
No embeds

No notes for slide

Ppt lesson 10

  1. 1. Microsoft Visual Basic 2005 BASICS Lesson 10 Do Loops
  2. 2. Microsoft Visual Basic 2005 BASICS 2 Objectives  Explain what a loop is.  Use the Do While and Do Until loops.  Use the InputBox function.  Use the Application.DoEvents statement.  Use nested loops.
  3. 3. Microsoft Visual Basic 2005 BASICS 3 What Are Loops?  Statement repetition is accomplished by a loop.  Iteration structure  The code required to create a loop  Loops  Do While  Do Until  For
  4. 4. Microsoft Visual Basic 2005 BASICS 4 Using the Do Loops  Knowing which of the Do loops to use is just a matter of experience.  Both types of Do loops rely on a condition to be either True or False.
  5. 5. Microsoft Visual Basic 2005 BASICS 5 Using Do While  Do While loop  Repeats a block of code as long as a condition remains True  Syntax example
  6. 6. Microsoft Visual Basic 2005 BASICS 6 Using Do While (cont.)
  7. 7. Microsoft Visual Basic 2005 BASICS 7 Using Do Until  Do Until loop  Repeats a block of code until a condition is no longer True  Syntax example
  8. 8. Microsoft Visual Basic 2005 BASICS 8 Using Do Until (cont.)
  9. 9. Microsoft Visual Basic 2005 BASICS 9 Choosing Do While or Do Until  Primary difference  A Do While loop tests the condition at the top of the loop.  The Do Until loop tests the condition at the bottom of the loop.  Do While loop  Should only be used if you are sure that you want the loop to execute at least once
  10. 10. Microsoft Visual Basic 2005 BASICS 10 Choosing Do While or Do Until (cont.)
  11. 11. Microsoft Visual Basic 2005 BASICS 11 Using the InputBox Function  InputBox function  The opposite of the MsgBox function  Displays a window to ask the user for input  Using the InputBox function  Supply two strings  The text that will prompt the user  The title for the window’s title bar
  12. 12. Microsoft Visual Basic 2005 BASICS 12 Using the InputBox Function (cont.)
  13. 13. Microsoft Visual Basic 2005 BASICS 13 Using the InputBox Function within a Do Until Loop  The InputBox function can be used inside a Do Until loop.  To repeatedly ask the user for data until a specified condition is met  Place a call to the InputBox function inside a Do Until loop.
  14. 14. Microsoft Visual Basic 2005 BASICS 14 Using the InputBox Function within a Do Until Loop (cont.)
  15. 15. Microsoft Visual Basic 2005 BASICS 15 Potential Event Procedure Problems and Solutions  When an event is triggered  An event procedure is executed to handle the event  Executing an event procedure takes some time.  Can cause problems
  16. 16. Microsoft Visual Basic 2005 BASICS 16 Event Procedure Problems  Long event procedures  Includes a loop that processes thousands of instructions  Program is unresponsive to other events  Endless loops  Condition to stop the loop never becomes True  Usually a programming error
  17. 17. Microsoft Visual Basic 2005 BASICS 17 Event Procedure Problems (cont.)
  18. 18. Microsoft Visual Basic 2005 BASICS 18 Event Procedure Solutions  Application.DoEvents subroutine  Allows computer to process other events even though the current event procedure is not yet complete  Add it to a loop that may occupy a lot of the computer’s time.  Introduces the potential for repetitive Click event procedures
  19. 19. Microsoft Visual Basic 2005 BASICS 19 Event Procedure Solutions (cont.)
  20. 20. Microsoft Visual Basic 2005 BASICS 20 Event Procedure Solutions (cont.)  Preventing repetitive Click event procedures  Disable the button at the beginning of the event procedure.  Prevents the user from clicking the button again until the event processing is completed
  21. 21. Microsoft Visual Basic 2005 BASICS 21 Event Procedure Solutions (cont.)
  22. 22. Microsoft Visual Basic 2005 BASICS 22 Using Nested Loops  Like If statements, loops may be nested.  It is not uncommon to need a loop within a loop.
  23. 23. Microsoft Visual Basic 2005 BASICS 23 Summary  Much of the work a computer does is repeated many times. This repetition in programs is accomplished using loops.
  24. 24. Microsoft Visual Basic 2005 BASICS 24 Summary (cont.)  A Do loop condition applies a test to achieve either a True or False result. A Do While loop tests the condition at the top of the loop and repeats a group of statements while a certain condition is True. A Do Until loop tests the condition at the bottom of the loop and repeats a group of statements until a certain condition becomes True. The code in a Do Until loop is always executed at least once.
  25. 25. Microsoft Visual Basic 2005 BASICS 25 Summary (cont.)  The InputBox function creates a window that prompts the user for input. To use the InputBox function, you supply the text for the prompt, the title for the window’s title bar, and the optional default text for the text box.  Sometimes, long event procedures can make a program unresponsive to other events.
  26. 26. Microsoft Visual Basic 2005 BASICS 26 Summary (cont.)  An endless loop is a loop in which the condition that stops the loop is never met. Clicking the Break All button in the IDE will pause a program with an endless loop and will highlight the code where the program stopped.
  27. 27. Microsoft Visual Basic 2005 BASICS 27 Summary (cont.)  An Application.DoEvents subroutine allows the program to process other events while an event procedure is executing.  Loops can be nested in the same way that If statements are nested.

×