27
Microsoft Visual Basic 2005 BASICS Lesson 10 Do Loops

Ppt lesson 10

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS

Lesson 10

Do Loops

Page 2: Ppt lesson 10

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.

Page 3: Ppt lesson 10

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

Page 4: Ppt lesson 10

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.

Page 5: Ppt lesson 10

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

Page 6: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 6

Using Do While (cont.)

Page 7: Ppt lesson 10

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

Page 8: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 8

Using Do Until (cont.)

Page 9: Ppt lesson 10

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

Page 10: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 10

Choosing Do While or Do Until (cont.)

Page 11: Ppt lesson 10

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

Page 12: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 12

Using the InputBox Function (cont.)

Page 13: Ppt lesson 10

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.

Page 14: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 14

Using the InputBox Function within a Do Until Loop (cont.)

Page 15: Ppt lesson 10

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

Page 16: Ppt lesson 10

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

Page 17: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 17

Event Procedure Problems (cont.)

Page 18: Ppt lesson 10

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

Page 19: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 19

Event Procedure Solutions (cont.)

Page 20: Ppt lesson 10

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

Page 21: Ppt lesson 10

Microsoft Visual Basic 2005 BASICS 21

Event Procedure Solutions (cont.)

Page 22: Ppt lesson 10

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.

Page 23: Ppt lesson 10

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.

Page 24: Ppt lesson 10

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.

Page 25: Ppt lesson 10

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.

Page 26: Ppt lesson 10

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.

Page 27: Ppt lesson 10

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.