Download presentation
Presentation is loading. Please wait.
Published byScott Jefferson Modified over 6 years ago
1
CIS 16 Application Development Programming with Visual Basic
Chapter Five Repeating Program Instructions
2
Objectives After studying this chapter, you should be able to:
Differentiate between a looping condition and a loop exit condition Differentiate between a pretest loop and a posttest loop Include pretest and posttest loops in pseudocode and in a flowchart Write a Do…Loop statement Utilize counters and accumulators
3
Objectives (cont'd.) Display a dialog box using the InputBox function
Use a text box’s Multiline, ReadOnly, and ScrollBars properties Include a list box in an interface Enable and disable a control Refresh the screen and delay program execution Use the Not logical operator
4
The Repetition Structure
Repetition structure, referred to more simply as a loop, need the computer to repeatedly process one or more program instructions. Repeating the instructions is referred to as the looping condition. The requirement for not repeating the instructions is referred to as the loop exit condition. Condition evaluated before the instructions is referred as pretest loop. Condition evaluated after the instructions is referred a posttest loop.
5
The Repetition Structure (cont'd.)
A pre-test loop is used to set up the loop In a pretest loop, the condition appears at the beginning of the loop, indicating that it is evaluated before the instructions within the loop are processed Depending on the result of the evaluation, the instructions in a pretest loop may never be processed
6
The Repetition Structure (cont'd.)
The Posttest_loop causes the loop to be performed at least once In a posttest loop, the condition appears at the end of the loop, indicating that it is evaluated after the instructions within the loop are processed Unlike the instructions in a pretest loop, the instructions in a posttest loop will always be processed at least once
7
The Do…Loop Statement Do…Loop statement
Used to code both a pretest loop and a posttest loop Two variations of syntax: one for a pretest loop and one for a posttest loop While keyword: Indicates that instructions should be processed while the condition is true Until keyword: Indicates that instructions should be processed until the condition becomes true
8
The Do…Loop Statement (cont.)
The Do…Loop statement begins with the Do clause and ends with the Loop clause Instructions to be repeated are placed between the Do and Loop clauses Use the While or Until keyword before the condition The condition must evaluate to Boolean True or False Location of the { While | Until } condition: Pretest loop: Appears in the Do clause Posttest loop: Appears in the Loop clause
9
Do While Loop (pre-test)
10
Do Until Loop (pre-test)
Until balance >
11
Do Until Loop (post-test)
Until balance >
12
Do While Loop (post-test)
While balance <
13
Overflow Errors An overflow error occurs when the value assigned to a memory location is too large for the location’s data type A loop that has no way to end itself is called an infinite loop or an endless loop
14
Counter-Controlled Loops
Loops whose instructions must be processed a precise number of times are referred to as counter-controlled loops The processing is controlled by a counter A counter is a numeric variable used for counting
15
The For…Next Statement
For...Next statement: processes a set of instructions a known number of times Is a pretest loop Needs a Counter variable that is used to keep track of the number of times the loop has been processed Startvalue, endvalue, and stepvalue items Control the number of times the loop is processed Must evaluate to numeric values Can be positive or negative
16
For Next Loop (cont'd.)
17
For Loop (cont'd.) Comparison of the For…Next and Do…Loop statements
18
Nested Repetition Structures
Repetition structures can be nested Can use pretest or posttest loops for outer loop and for inner (nested) loop A clock with minute and second hands demonstrates nested loops Minute hand moves 1 position, then the second hand moves 60 positions
19
Nesting using selection structure
20
Nested Repetition Structures
21
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Input box
22
The InputBox Function InputBox
Used to prompt the user to enter some specific information while an application is running consists of a message asking for input, an input area, a title, an OK button, and a Cancel button The value returned by the InputBox function depends on the button the user chooses If the user clicks the OK button, the function returns the string value contained in the input area of the dialog box If the user clicks either the Cancel button in the dialog box or the Close button on the dialog box’s title bar, the function returns an empty (or zero-length) string
23
The InputBox Function (cont'd.)
24
The InputBox Function (cont'd.)
25
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Textbox and listbox
26
Displaying Lists Two controls available to display lists of data
Listbox – designed to display a list, and user can select items from the list, similar to a drop down list Textbox – change properties to simulate a list
27
Textbox properties A TextBox often has Multiline and ReadOnly properties set to True, and its ScrollBars property set to Vertical With the Multiline property set to True, the text box can both accept and display multiple lines of text; otherwise, only one line of text can be entered in the text box Changing a text box’s ReadOnly property from its default value (False) to True prevents the user from changing the contents of the text box during run time A text box’s ScrollBars property specifies whether the text box has no scroll bars (the default), a horizontal scroll bar, a vertical scroll bar, or both horizontal and vertical scroll bars
28
ListBox A list box displays a list of items
The user can select zero items, one item, or multiple items The number of items the user can select is controlled by the list box’s SelectionMode property The default value for the property, One, allows the user to select only one item at a time
29
ListBox (cont'd.) The items in a list box belong to a collection called the Items collection A collection is a group of individual objects treated as one unit The first item in the Items collection appears as the first item in the list box The second item in the collection appears as the second item in the list box, and so on
30
List Box (cont.) Adding Items to a List Box Collection
A group of objects treated as one unit Items collection Refers to a group of items in a list box Index A unique number that identifies each item in a collection The first item has an index of 0 The Items collection’s Add method Used to add an item to a list box Usually entered in the form’s Load event procedure
31
Including a ListBox in an Interface (cont'd.)
32
Populating the ListBox
Populate a list box at runtime using the forms Load event Useful if data is in a database table Any code contained in the Load event procedure is processed before the form is displayed on the screen
33
Populating the Listbox (cont'd.)
Using another control as the data source
34
Populating the ListBox (cont'd.)
Using data obtained from the user
35
Clearing a ListBox You can use the Items collection’s Clear method to clear the items from a list box
36
Sorting a ListBox The position of an item in a list box depends on the value stored in the list box’s Sorted property When the Sorted property is set to False (the default value), the item is added at the end of the list When it is set to True, the item is sorted and then placed in its proper position in the list
37
Determining the Number of Items in a List Box
The number of items in a list box is stored in the Items collection’s Count property The property’s value is always one number more than the last index in the list box; this is because the first index in a list box is 0
38
Accessing Items in a List Box
Each item in the Items collection is identified by a unique number, which is called an index The first item in the collection (which also is the first item in the list box) has an index of 0 The second item’s index is 1, and so on The index allows you to access a specific item in the list box
39
The SelectedItems and SelectedIndex Properties
You can use either the SelectedItem property or the SelectedIndex property to determine whether an item is selected in a list box When an item is NOT selected: SelectedItem property contains the empty string SelectedIndex property contains the number −1 (negative 1) When an item is selected: SelectedItem property contains the value of the selected item SelectedIndex properties contains the item’s index The selected item is called the default list box item
40
The SelectedItems and SelectedIndex Properties (cont'd.)
41
The SelectedItems and SelectedIndex Properties (cont'd.)
42
Listbox Events Each time either the user or a statement selects an item in a list box, the list box’s SelectedValueChanged event occurs followed by its SelectedIndexChanged event You can use the procedures associated with these events to perform one or more tasks when the selected item has changed
43
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Financial class
44
The Financial Class d.)
45
The Financial.Pmt Method
Calculates a periodic payment on a loan or investment Returns the periodic payment as a Double type value Rate and number of periods arguments must be expressed in the same units (monthly, annual, etc.)
46
The Financial.Pmt Method (cont'd.)
47
Microsoft Visual Basic 2012: Reloaded, Fifth Edition
Refresh and sleep
48
The Refresh and Sleep Methods
Refresh method Ensures that the computer processes any previous lines of code that affect the interface’s appearance Syntax: Me.Refresh() Me refers to the current form Sleep method Delays program execution Syntax: System.Threading.Thread.Sleep(milliseconds) Millisecond: 1/1000 of a second
49
This weeks Assignments
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.