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- 10 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. 7- 11 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. 7- 12 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. 7- 13 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. 7- 14 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. 7- 15 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. 7- 16 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. 7- 17 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. 7.5 The ListBox and ComboBox Controls (cont.) 7- 18 – 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. 7.5 The ListBox and ComboBox Controls (cont.) 7- 19 – Events • • • • • DoubleClick KeyPress Enter Leave SelectIndexChanged McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 7.5 The ListBox and ComboBox Controls (cont.) 7- 20 – 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. 7.5 The ListBox and ComboBox Controls (cont.) 7- 21 • 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. 7- 22 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. 7.6 Collections and the For Each…Next Structure (cont.) 7- 23 – 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. 7- 24 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. 7- 25 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. 7- 26 7.7 The ListView Control (cont.) – Events • Responds to both the DoubleClick and SelectedIndexChanged events. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 27 Chapter Summary • Loop structures are used to execute a group of statements repeatedly. • Two basic types of loops 1. Do…Loop – Number of iterations unknown at the start. 2. For…Next Loop – Number of iterations known at the beginning. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 28 Chapter Summary (cont.) • Two variations of the Do…Loop. – Do While Loop • Condition evaluated at the beginning of loop. – Do…Loop While • Condition evaluated at the end of loop. • Two other variations of the Do…Loop – Do Until…Loop – Do…Loop Until – Identical to the While except the condition used to control the loop is negated. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 29 Chapter Summary (cont.) • Statements inside the loop must change the values of the variables involved in the condition. • The For…Next loop uses a counter variable to control the number of iterations. • Variations of the Do…Loop can be terminated using the Exit Do statement. • The body of the loop can contain another loop. • The ListBox control presents the user with a list of items that can be selected. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 7- 30 Chapter Summary (cont.) • The ComboBox control combines the features of a ListBox and a TextBox. • Both ListBox and ListView components provide properties to access items selected by the user. • A collection stores ListBox and ListView properties. • Use a For…Each structure to process a collection. • The ListView component provides a way to display a list of icons and text in tabular form. McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved.
© Copyright 2026 Paperzz