Presentation is loading. Please wait.

Presentation is loading. Please wait.

Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.

Similar presentations


Presentation on theme: "Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed."— Presentation transcript:

1 Iterations (aka Loops)

2 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.

3 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.

4 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

5 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

6 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

7 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

8 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.

9 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

10 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.

11 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

12 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

13 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.

14 Controls for Lists ListBox and ComboBox

15 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.

16 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

17 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

18 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?

19 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

20 20 Display an Iteration A ListBox provides a handy visual representation of an iteration (loop).  DisplayIteration

21 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

22 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

23 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).

24 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.

25 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

26 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 YesNo DropDownStyle = DropDown Yes DropDownStyle = DropDownList No; really acts like a ListBox Yes

27 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

28 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

29 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.

30 30 Removing an Item Remove an item with a given item value objName.Items.Remove( item value ); Remove an item at a given index position objName.Items.RemoveAt( index );

31 31 Clearing a List Removes all items from list objName.Items.Clear();

32 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();

33 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?


Download ppt "Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed."

Similar presentations


Ads by Google