iterations (aka loops). 2 loops loops (iterations) are segments of code that may be executed several...
DESCRIPTION
3 for Loop The for loop is a fixed-count (definite) iteration. The loop is controlled by the value of a block scope integer variable. The integer is incremented each time the loop executes until its value reaches a preset limit. The initial value of the integer is set at design time.TRANSCRIPT
![Page 1: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/1.jpg)
Iterations
(aka Loops)
![Page 2: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/2.jpg)
2
Loops
• Loops (iterations) are segments of code that may be executed several times.
• Fixed-count (definite) loops repeat a fixed number of times, determined by the code.
• Variable-count (indefinite) loops repeat a variable number of times, determined by the actions of the user or variable value.
![Page 3: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/3.jpg)
3
for Loop
• The for loop is a fixed-count (definite) iteration.• The loop is controlled by the value of a block
scope integer variable.• The integer is incremented each time the loop
executes until its value reaches a preset limit.• The initial value of the integer is set at design
time.
![Page 4: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/4.jpg)
4
for Loop
• The “mechanism” that controls the loop is contained within parentheses:– Initialize block scope variable– Condition– Increment/decrement block scope variable
• Any code to be iterated by the loop is contained within braces.
ForToOutput
![Page 5: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/5.jpg)
5
Incrementing Block Scope Variable
• To count down, rather than up, use a decrementer.
• To count either up or down with increments other than 1, use the appropriate assignment operator.
ForCountDown
ForCountBy
![Page 6: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/6.jpg)
6
Early Exit
• Even though the for loop is programmed to iterate a fixed number of times, special circumstances may dictate an early exit.
• The iterated code can check for a certain condition.
• If the condition is met, the break statement will stop the loop.
ForWithBreak
![Page 7: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/7.jpg)
7
Nested for Loop
• A second iteration may be nested within the first with a for loop.
• The inner for loop iterates completely for each increment of the outer for loop.
NestedForToOutput
![Page 8: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/8.jpg)
8
while Loop
• The while loop is an variable-count (indefinite) iteration.
• Once launched, the iterated code executes until the condition is no longer true.
• The condition is enclosed in parentheses.• User input or some variable altering statement
should force the condition to turn false so that the loop will be exited.
![Page 9: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/9.jpg)
9
while Loop
• Code to be iterated in enclosed in braces.• With a while loop, the condition is checked
before each iteration or at the top of the iteration.
• If the condition is never true, the iterated code will not be executed.
SimpleWhile
![Page 10: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/10.jpg)
10
Transfer of Focus
• If an iteration is started from a button click handler function, it is a good idea to transfer focus to the control that will raise the event to stop the iteration.
• If this is not done, the stop button may have to be clicked twice before it raises its event.
• Use the Focus() method to transfer focus.
![Page 11: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/11.jpg)
11
User Intervention
• This demonstration shows the steps necessary to start and stop an indefinite iteration with user intervention.
• Calls to Application.DoEvents() and Focus() are necessary.
WhileToOutput
![Page 12: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/12.jpg)
12
do … while Loop
• A do while loop is a variable-count (indefinite) iteration, where the condition is checked at the bottom of the loop.
• The iterated code is placed in braces after the introductory keyword do.
DoWhile
ForWhile
![Page 13: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/13.jpg)
13
Remember…
• Because the condition for completion of the do…while loop follows the instructions, the loop will always be executed at least once, regardless of the state of the stop variable.
![Page 14: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/14.jpg)
Controls for Lists
ListBox and ComboBox
![Page 15: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/15.jpg)
15
ListBox Control
• The ListBox is a scrollable control that displays a list of text values.
• Use the lst prefix for the ListBox.• The values in the list are stored in an Items
Collection, which is a 0-based array.• Depending on the size of the ListBox control on
the form, all or some of list items will display; scrollbars are automatically added to provide access to all items in ListBox.
![Page 16: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/16.jpg)
16
Selecting an Item
• Clicking (or highlighting) an item in a ListBox raises the SelectedIndexChanged event and assigns that item to the SelectedItem Property (in text format).
• The position of the item in the ListBox becomes the SelectedIndex Property (the index of the item in integer format).
• Assigning a value to the SelectedIndex Property causes the corresponding item to be highlighted.
ListSelection
![Page 17: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/17.jpg)
17
Selecting an Item
• If no list item is selected, value of the SelectedIndex Property is -1.
• Items can be selected at run-time by setting the SelectedIndex Property.
SelectListItem
![Page 18: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/18.jpg)
18
Highlighting Items at Run-Time
• You may choose to highlight a default choice in a list at run-time.
• You can put code into the Load event handler for the form. This event “fires” before the form appears on the screen.
• ?How would you set the first item in the Cities ListBox to be automatically highlighted when the form is displayed?
• ?How would you set the last choice in the Flavors ComboBox to be automatically highlighted when the form is displayed?
![Page 19: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/19.jpg)
19
The Items Collection
• The list of text items comprises an object generally described as a Collection; think of it as a 0-based array with associated properties and methods.
• An Items Collection has a Count Property• Items have Add(), Remove(), RemoveAt(),
Insert(), Contains() and Clear() methods.• These methods can be used to manipulate the
Items collection of a ListBox at run- time.
ListItemsCollection
![Page 20: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/20.jpg)
20
Display an Iteration
• A ListBox provides a handy visual representation of an iteration (loop).
DisplayIteration
![Page 21: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/21.jpg)
21
SelectionMode Property
• Set to One to allow only one item to be selected at a time.
• Set to MultiSimple to allow selecting multiple items, one at a time.
• Set to MultiExtended to allow selecting a range of items with click, shift, click.
• Multiple selections comprise a SelectedItems Collection.
SelectionModes
![Page 22: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/22.jpg)
22
SelectedItems Collection
• The SelectedItems collection has a Count Property that makes possible iterating through it with a for loop.
• This example loads an array with an iteration through the SelectedItems Collection.
• Display the array in a second ListBox.
SelectedItemsCollection
![Page 23: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/23.jpg)
23
Events
• Textchanged --- for each keystroke typed, a Textchanged event “fires”.
• Enter --- when control receives focus, the Enter event “fires”.
• Leave --- when control loses focus, the Leave event “fires” before next control gets focus.
• SelectedIndexChanged --- an item is selected(highlighted).
![Page 24: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/24.jpg)
24
ComboBox Control
• This is actually a composite of two controls: the ListBox and TextBox controls.
• A primary feature is that the selected item from the list appears in the TextBox portion of the control as the Text Property.
• This obviously precludes multiple selection modes with the ComboBox.
• Use the prefix, cbo, when naming a ComboBox.
![Page 25: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/25.jpg)
25
Alternate Style ComboBoxes
• The default style ComboBox (DropDownStyle = DropDown) features a an arrow that allows you to expand the list, only when needed,which is a significant space-saver in interface design.
• In a ComboBox with DropDownStyle = Simple, the list portion of the control stays open.
• In a ComboBox with a DropDownStyle = DropDownList, the TextBox portion of the control becomes read only.
AlternateCombos
![Page 26: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/26.jpg)
26
ComboBox DropDownStyle Property
• Determines whether the ComboBox drops down and whether text can be entered.
Text Can Be Entered?
List “Drops Down”?
DropDownStyle = Simple
Yes No
DropDownStyle = DropDown
Yes Yes
DropDownStyle = DropDownList
No; really acts like a ListBox
Yes
![Page 27: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/27.jpg)
27
ComboBox Text Entry
• The TextBox portion of the control allows text entry.
• This allows entry of an item that is not part of the list.
• However, entering text in the TextBox portion does not automatically cause it to appear in the list.
SimpleCombo
![Page 28: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/28.jpg)
28
Adding the Text Property to the List
• A bit of extra coding, making use of the Contains() and Add() method of the Items Collection can cause unique entries in the TextBox portion to be added to the list.
• Because the Contains() method is case sensitive, it is possible to add duplicate entries, with differing cases using this technique.
ComboAdd
![Page 29: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/29.jpg)
29
Ways to “Fill” the ListBox/ComboBox
• During design-time, type the list items, one to a line, using the Collection Editor.
• At run-time, use the objName.Items.Add(item value) or objName.Items.Insert(index, item value) to “populate” the list.
• At run-time, the list can be “populated” from a data file or an existing array by setting the DataSource Property of the ListBox or ComboBox.
• Sorted Property = true keeps items in alphabetical order.
![Page 30: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/30.jpg)
30
Removing an Item
• Remove an item with a given item valueobjName.Items.Remove(item value);
• Remove an item at a given index positionobjName.Items.RemoveAt(index);
![Page 31: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/31.jpg)
31
Clearing a List
• Removes all items from listobjName.Items.Clear();
![Page 32: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/32.jpg)
32
How to Highlight Text in a Control
• To highlight text in a control, use the SelectAll() method.
• It is often convenient to SelectAll() at the same time you give focus to a control, so that the text is ready to be replaced.txtName.Focus();txtName.SelectAll();
![Page 33: Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed](https://reader035.vdocuments.us/reader035/viewer/2022062302/5a4d1b227f8b9ab059995ead/html5/thumbnails/33.jpg)
33
Iterating Through a List
• Suppose you want to “print” a list of all items in a list to a MessageBox.
• You could use a string variable to accumulate the item values.
• You could use a for loop to “print” 1 item at a time to this string variable.
• ?How to “Print” a list of all flavors in Flavors ListBox to a MesssageBox?