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

Slides:



Advertisements
Similar presentations
Lists, Loops, Validation, and More
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 5- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Microsoft Visual Basic: Reloaded Chapter Seven More on the Repetition Structure.
CS0004: Introduction to Programming Repetition – Do Loops.
Visual Basic.NET BASICS Lesson 10 Do Loops. 2 Objectives Explain what a loop is. Use the Do While and Do Until loops. Use the InputBox function. Use the.
C# Programming: From Problem Analysis to Program Design1 9 Programming Based on Events.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Programming Based on Events
ListBoxes The list box control allows the user to view and select from multiple items in a list. CheckedListBox control extends a list box by including.
Group Boxes and Panels Arrange components on a GUI Buttons and etc. can be placed inside a group box or panel. All these buttons move together when the.
List-based Controls. Slide 2 Introduction There are several controls that work with lists ComboBox ListBox CheckedListBox.
An Object-Oriented Approach to Programming Logic and Design Chapter 6 Looping.
Chapter 8 Using Repetition with Loops and Lists. Class 8: Loops and Lists Write Do loops to execute statements repeatedly Write For loops to execute statements.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
CHAPTER 6 Loop Structures.
Chapter 5 Java Script And Forms JavaScript, Third Edition.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Chapter 7 Lists, Loops, and Printing Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
1 Graphical User Interfaces Part 2 Outline ListBoxes and CheckedListBoxes ListBoxes CheckedListBoxes ComboBoxes.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
6.3 List Boxes and Loops Some Properties, Methods, and Events of List Boxes List Boxes Populated with Strings List Boxes Populated with Numbers Searching.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
5-1 Chapter 5 The Repetition Process in VB.NET. 5-2 Learning Objectives Understand the importance of the repetition process in programming. Describe the.
CPS120 Introduction to Computer Science Iteration (Looping)
ListBox, ComboBox, Menu Chapter 5.4, ComboBox Control: Properties & Methods u Combines TextBox features with a short drop- down list  cboOne.AddItem(string)
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Chapter 6: The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
Visual Basic Programming
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
1.
Introduction to Problem Solving and Control Statements.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
CPS120 Introduction to Computer Science Iteration (Looping)
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al- ajmi Chapter 3 Some Visual Basic Controls and Events Visual Basic. NET.
AdditionalControls 1. The MenuStrip 2 Double-click Let’s begin to design the menu bar for VB! Let’s begin to design the menu bar for VB! 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.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
List Boxes and Combo Boxes Provides a list of items to select from Various styles — choose based on Space available Need to select from an existing list.
Controlling Program Flow with Looping Structures
Unit 6 Repetition Processing Instructor: Brent Presley.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
COMPUTER PROGRAMMING I Apply Procedures to Develop List Box and Combo Box Objects.
Microsoft Visual Basic 2008: Reloaded Third Edition
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Programming Logic and Design Fourth Edition, Comprehensive
Apply Procedures to Develop Menus, List Box and Combo Box Objects
Repeating Program Instructions
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS16 Application Development and Programming using Visual Basic.net
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Lecture Set 10 Windows Controls and Forms
Presentation transcript:

Iterations (aka Loops)

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

Controls for Lists ListBox and ComboBox

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

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 Display an Iteration A ListBox provides a handy visual representation of an iteration (loop).  DisplayIteration

Test Yourself? How would you set the last choice in the Flavors ListBox to be automatically highlighted when the form is displayed? 21

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 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 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 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 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 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 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 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 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 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 Clearing a List Removes all items from list objName.Items.Clear();

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

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