Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1 Iterations (aka Loops)

2 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat a fixed number of times, determined by loop control variable. 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 in the first part (initialization section) of the for statement.

4 4 for Loop The “mechanism” that controls the loop is contained within parentheses: –Initialize block scope variable –Condition –Increment/decrement block scope variable The loop body is generally contained within braces. If the loop body consists of just 1 statement, braces are not required.  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 loop body 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 does all the iterations of the nested loop 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 loop body 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 The loop body is generally contained within braces. If the loop body consists of just 1 statement, braces are not required. 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 loop body will never 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 an object that stores list values in 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 User Selection of 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). If no items were originally selected as the default and the user does not select an item, the SelectedItem Property of the ListBox will be -1.  ListSelection

17 17 Selecting an Item via Code If no list item is to appear as selected, set the value of the SelectedIndex Property to -1. You may choose to highlight a default choice in a list at run-time. To have a default item appear highlighted when the form opens, assign the appropriate integer value to the SelectedIndex Property of the ListBox in the Load event handler for the form. This event “fires” before the form appears on the screen.  SelectListItem

18 18 Test Yourself? How would you set the first item in the Payment ListBox 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 Test Yourself? How would you set the last choice in the Flavors ListBox to be automatically highlighted when the form is displayed? 21

22 22 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

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

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

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

26 26 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

27 27 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

28 28 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

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

30 30 Ways to “Fill” the ListBox/ComboBox 1.During design-time, type the list items, one to a line, using the Collection Editor. 2.At run-time, use the objName.Items.Add( item value ) or objName.Items.Insert( index, item value ) to “populate” the list. 3.At run-time, the list can be “populated” from a data file or an existing array (which we’ll cover later). Sorted Property = true keeps items in alphabetical order.

31 31 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 );

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

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

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

35 Test Yourself? How would you “Print” a list of all soft drink names in the SoftDrink ListBox to a MesssageBox? 35


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

Similar presentations


Ads by Google