After completing this unit, you should be able to 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.
CHAPTER SEVEN Repeating Processing Tasks Loop Structures 7- 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- 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- 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- 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- 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.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- 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- 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.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 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 726 Chapter Summary • Loop structures are used to execute a group of statements repeatedly • Two basic types of loops Do…Loop – Number of iterations unknown at the start For…Next Loop – Number of iterations known at the beginning McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 727 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 728 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 729 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 730 ... reserved 7- 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:... reserved 72 6 Chapter Summary • Loop structures are used to execute a group of statements repeatedly • Two basic types of loops Do Loop – Number of iterations unknown at the start For…Next Loop – Number... reserved 72 7 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