Tải bản đầy đủ (.ppt) (30 trang)

Lecture Learning programming using Visual Basic Net – Chapter 7 Repeating processing tasks Loop structures

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (116.95 KB, 30 trang )

CHAPTER SEVEN
Repeating Processing Tasks
Loop Structures


7- 2

Introduction

• Most applications certain processing steps
repeatedly.
• This structure or flow of control is known as a
loop.
• The Do…Loop structure executes a block of
statements until a specified condition occurs.
• The For…Next structure executes a block of
statements a specified number of times.
McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


7- 3

Objectives







Use the Do…Loop and For…Next structure.
Know when each structure is appropriate.
Understand the operation of nested loops.
Construct user interfaces with the ListBox,
ComboBox, and ListView controls.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


7- 4

7.1 The Do…Loop Structure
• Syntax and Action of the Do…Loop
Do

statementblock
Loop
– Run Time: The Effect of the Do…Loop Structure

• Statements in a loop’s interior are executed
one at a time.
• The Do marks the top of the loop.
• The Loop sends the execution back to the top.
McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All

rights reserved.


7- 5

7.1 The Do…Loop Structure (cont.)
• Do While…Loop
Do While condition

statementblock
Loop
– Run Time: The Effect of the Do While…Loop Structure

• Loop continues to execute “while the condition
is true.”
• Loop is exited when the condition is false.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


7- 6

7.1 The Do…Loop Structure (cont.)


Coding the Loop Body



One or more statements in the loop body must eventually
cause the condition to become false.



Order of statements inside loop affects the result of the
loop.



Loop may terminate immediately.



Loop can be viewed as a meta statement.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


7- 7

7.1 The Do…Loop Structure (cont.)
• Do Loop…While
– Works like the Do While…Loop.
– Termination at the bottom of the loop.
– Syntax:


Do

statementblock
Loop While condition

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


7- 8

7.1 The Do…Loop Structure (cont.)
• Do Until…Loop and Do…Loop Until
– Do Until…Loop

• Termination condition at the top of the loop.
• Same syntax and action as the Do While…
Loop.

– Except:
» Difference in keywords.
» Loop statements inside the Do Until…Loop are
executed when the conditions are False.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All

rights reserved.


7- 9

7.1 The Do…Loop Structure (cont.)
– Do…Loop Until

• Termination condition at the bottom of the loop.
• Exit when the condition is True.

• Initialization and Termination
• Loops may start or end at the wrong place.

– The initialization statement may be off.
– The condition operator may need to be
changed.
– The order of statements within the loop
may need to be changed.
McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


7.2 The For…Next Loop Structure
• Syntax and Action of For…Next
For counter = start To end

statementblock

Next
– Run Time: The Effect of the For…Next Structure

• Counter variable and start and end expressions
must be evaluated to control the number of
times the loop will be processed.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

710


7.2 The For…Next Loop Structure (cont.)
• For…Next versus Do…Loop Variations
– Use a For…Next loop when the number of loop iterations
can be determined.
– Use a Do…Loop variety only when the number of iterations
depends on the results of statements inside the loop.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

711



7.2 The For…Next Loop Structure (cont.)
• The Step Amount
– Counter variable may be incremented by a step amount,
such 3 instead of 1.
– Syntax:

For counter = start To end Step increment

statementblock
Next

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

712


7.2 The For…Next Loop Structure (cont.)

• Avoiding For…Next Errors
– Do not place statements inside the loop that change start,
end, and increment expressions.
– Do not change the counter variable inside the loop.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.


713


7.3 Exit Do and Exit For

• Exit Do is used inside a Do…Loop structure to
exit the loop immediately.
• Exit For is used inside a For…Next structure to
exit the loop immediately.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

714


7.4 Nested Loops
• Loops may be embedded inside of other loops.
• Nested Do While…Loops
Do While condition1
statementblock1a

Do While condition2
statementblock2

Loop


statementblock1b
Loop

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

715


7.4 Nested Loops (cont.)
• Nested For…Next Loops
For counter1 = start1 To end1
statementblock1a

For counter2 = start2 To end2
statementblock2

Next

statementblock1b
Next

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

716



7.5 The ListBox and ComboBox Controls
• The ListBox
– Display a list of items from which the user makes their
selection.
– Appearance and Use

• Rectangle that displays rows of text.
• Vertical scroll bar is automatically provided, if
necessary.
• A means to obtain input and output.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

717


7.5 The ListBox and ComboBox Controls
(cont.)
– Properties

HorizontalScrollbar
Items
MultiColumn
SelectedIndex
SelectedIndices

SelectedItem

McGraw Hill/Irwin

SelectedItems
SelectionMode
Sorted
Text
UseTabStops

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

718


7.5 The ListBox and ComboBox Controls
(cont.)
– Events







DoubleClick
KeyPress
Enter
Leave

SelectIndexChanged

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

719


7.5 The ListBox and ComboBox Controls
(cont.)
– Methods

• Two common used

– FindString and FindStringExact
• Selected methods of the Items property

– Add(text)
– Insert(index, text)
– Remove(text)
– RemoveAt(index)
– Clear()
McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

720



7.5 The ListBox and ComboBox Controls
(cont.)
• The ComboBox
– Combination TextBox/Listbox
– Properties

• Has many of the TextBox and ListBox
properties.
• The three styles of the ComboBox are
controlled by the DropDownStyle property.
– Events

• Similar to the ListBox.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

721


7.6 Collections and the For Each…Next Structure
• A Collection object is an ordered set of items
that can be referred to as a unit.
– The ListBox provides collections.
– The ListBox permits either single or multiple item
selection.


• The For Each…Next Structure
– Repeats a group of statements for each element
in a collection.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

722


7.6 Collections and the For Each…Next
Structure (cont.)
– Syntax of the For Each…Next structure

For Each object In collection
statementblock
Next

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

723


7.7 The ListView Control


• Used to display lists of items that can include
icons and/or text.
– Window Explorer uses a ListView control.
– Appearance and Use

• Appears as a rectangular area that can contain
icons, text, or combinations of icons and text.
• Can display textual information in a row and
column format.

McGraw Hill/Irwin

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

724


7.7 The ListView Control (cont.)

– Properties

CheckBoxes
CheckedIndices
CheckedItems
FullRowSelect
GridLines
Items


McGraw Hill/Irwin

MultiSelect
Scrollable
SelectedIndices
SelectedItems
Sorting
View

©2002 by The McGraw-Hill Companies, Inc. All
rights reserved.

725


×