Ppt lesson 10

Post on 29-Nov-2014

1692 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

Transcript

Microsoft Visual Basic 2005 BASICS

Lesson 10

Do Loops

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.

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

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.

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

Microsoft Visual Basic 2005 BASICS 6

Using Do While (cont.)

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

Microsoft Visual Basic 2005 BASICS 8

Using Do Until (cont.)

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

Microsoft Visual Basic 2005 BASICS 10

Choosing Do While or Do Until (cont.)

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

Microsoft Visual Basic 2005 BASICS 12

Using the InputBox Function (cont.)

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.

Microsoft Visual Basic 2005 BASICS 14

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

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

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

Microsoft Visual Basic 2005 BASICS 17

Event Procedure Problems (cont.)

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

Microsoft Visual Basic 2005 BASICS 19

Event Procedure Solutions (cont.)

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

Microsoft Visual Basic 2005 BASICS 21

Event Procedure Solutions (cont.)

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.

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.

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.

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.

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.

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.

top related