visual basic.net basics lesson 10 do loops. 2 objectives explain what a loop is. use the do while...

16
Visual Basic .NET BASICS Lesson 10 Do Loops

Upload: jeffry-ryan

Post on 18-Dec-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

Visual Basic .NET BASICS

Lesson 10

Do Loops

Page 2: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

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: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

3

What Are Loops?

When a program repeats a group of statements a number of times, the repetition is accomplished by a loop.

The code required to create a loop is sometimes called an iteration structure.

Page 4: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

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: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

5

Using Do While

A Do While loop is used when you want a block of code to repeat as long as a condition remains True.

Page 6: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

6

Using Do Until

A Do until loop is used when you want a block of code to repeat until a condition is no longer True.

Page 7: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

7

Choosing Do While or Do Until

One of the primary differences between the two kind of Do loops is where the condition is tested.

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.

A Do While loop should only be used if you are sure that you want the loop to execute at least once.

Page 8: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

8

Using the InputBox Function

The InputBox function is basically the opposite of the MsgBox function.

The InputBox function displays a window to ask the user for input.

To use the InputBox function, you must supply two strings: the text that will prompt the user and the title for the window’s title bar.

Page 9: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

9

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.

This can be accomplished by placing a call to the InputBox function inside a Do Until loop.

Page 10: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

10

Endless Loops

In an endless loop, the condition that is supposed to stop the loop from repeating never becomes True.

Inside every loop, there should be code that causes a change that will eventually lead to the end of the looping.

If no such code exists inside the loop, the loop may repeat endlessly.

Page 11: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

11

The Application.DoEvents Statement

The Application.DoEvents subroutine allows the computer to process other events, even though the current event procedure is not yet complete.

By adding the Application.DoEvents subroutine inside the loop that may occupy a lot of the computer’s time, you make it possible to handle other events while the loop is finishing its work.

Page 12: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

12

Using Nested Loops

Like If statements, loops may be nested.

It is not uncommon to need a loop within a loop.

Page 13: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

13

Summary

Much of the work a computer does is repeated many times. This repetition in programs is accomplished using loops.

A Do loop condition applies a test to deter-mine 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.

Page 14: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

14

Summary (continued)

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.

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.

Page 15: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

15

Summary (continued)

Sometimes long event procedures can make a program unresponsive to other events.

An endless loop is a loop in which the con-dition which stops the loop is never met. Pressing Ctrl+Alt+Break will pause a pro-gram with an endless loop and will highlight the code where the program stopped.

Page 16: Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the

16

Summary (continued)

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