CIS 16 Application Development Programming with Visual Basic

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.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Chapter 7: Sub and Function Procedures
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
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.
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
Programming with Microsoft Visual Basic th Edition CHAPTER SEVEN SUB AND FUNCTION PROCEDURES.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
1 Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops 6.3 List Boxes and Loops.
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
Chapter 12: How Long Can This Go On?
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Programming Logic and Design Fifth Edition, Comprehensive
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
Chapter 6: The Repetition Structure
Tutorial 51 Programming Structures Sequence - program instructions are processed, one after another, in the order in which they appear in the program Selection.
Tutorial 6 The Repetition Structure
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter Six The Do Loop and List Boxes.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
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.
Chapter 5: More on the Selection Structure
1.
Chapter 15 I’m on the Inside; You’re on the Outside (Nested Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 16 I’m on the Inside; You’re on the Outside.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Controlling Program Flow with Looping Structures
Unit 6 Repetition Processing Instructor: Brent Presley.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
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 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 1 Unit 9 Do Until and For… Next Loops Chapter 5 Lists,
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
An Introduction to Programming with C++ Sixth Edition Chapter 8 More on the Repetition Structure.
Microsoft Visual Basic 2008: Reloaded Third Edition
Chapter 8: More on the Repetition Structure
REPETITION CONTROL STRUCTURE
Clearly Visual Basic: Programming with Visual Basic nd Edition
Lists, Loops, Validation, and More
IS 350 Loops.
Repeating Program Instructions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Microsoft Visual Basic 2005: Reloaded Second Edition
Objectives After studying this chapter, you should be able to:
Introduction to Problem Solving and Control Statements
CIS16 Application Development and Programming using Visual Basic.net
Topics Introduction to Repetition Structures
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Presentation transcript:

CIS 16 Application Development Programming with Visual Basic Chapter Five Repeating Program Instructions

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

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

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.

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

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

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

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

Do While Loop (pre-test)

Do Until Loop (pre-test) Until balance > 100000

Do Until Loop (post-test) Until balance > 100000

Do While Loop (post-test) While balance < 100000

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

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

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

For Next Loop (cont'd.)

For Loop (cont'd.) Comparison of the For…Next and Do…Loop statements

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

Nesting using selection structure

Nested Repetition Structures

Microsoft Visual Basic 2012: Reloaded, Fifth Edition Input box

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

The InputBox Function (cont'd.)

The InputBox Function (cont'd.)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition Textbox and listbox

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

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

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

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

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

Including a ListBox in an Interface (cont'd.)

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

Populating the Listbox (cont'd.) Using another control as the data source

Populating the ListBox (cont'd.) Using data obtained from the user

Clearing a ListBox You can use the Items collection’s Clear method to clear the items from a list box

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

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

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

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

The SelectedItems and SelectedIndex Properties (cont'd.)

The SelectedItems and SelectedIndex Properties (cont'd.)

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

Microsoft Visual Basic 2012: Reloaded, Fifth Edition Financial class

The Financial Class d.)

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

The Financial.Pmt Method (cont'd.)

Microsoft Visual Basic 2012: Reloaded, Fifth Edition Refresh and sleep

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

This weeks Assignments