Matakuliah Tahun : T0063 – Pemrograman Visual : 2009 Repeating Processing Tasks Loop Structures CHAPTER SEVEN 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. Bina Nusantara University 2 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. Bina Nusantara University 3 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. Bina Nusantara University 4 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. Bina Nusantara University 5 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. Bina Nusantara University 6 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 Bina Nusantara University 7 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. Bina Nusantara University 8 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. Bina Nusantara University 9 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 Bina Nusantara University 10 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. Bina Nusantara University 11 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 Bina Nusantara University 12 7.4 Nested Loops (cont.) • Nested For…Next Loops For counter1 = start1 To end1 statementblock1a For counter2 = start2 To end2 statementblock2 Next statementblock1b Next Bina Nusantara University 13 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. Bina Nusantara University 14 7.5 The ListBox and ComboBox Controls (cont.) – Properties • HorizontalScrollbar • Items • MultiColumn • SelectedIndex • SelectedIndices • SelectedItem Bina Nusantara University SelectedItems SelectionMode Sorted Text UseTabStops 15 7.5 The ListBox and ComboBox Controls (cont.) – Events • DoubleClick • KeyPress • Enter • Leave • SelectIndexChanged Bina Nusantara University 16 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. Bina Nusantara University 17 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. Bina Nusantara University 18 7.6 Collections and the For Each…Next Structure (cont.) – Syntax of the For Each…Next structure For Each object In collection statementblock Next Bina Nusantara University 19
© Copyright 2026 Paperzz