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

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.
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
COMPUTER PROGRAMMING I Essential Standard 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Do/Loops A loop repeats a series of instructions. An iteration is a single execution of the statement(s) in the loop. Used when the exact number of iterations.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Chapter 5: Loops and Files.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
Chapter 5 new The Do…Loop Statement
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
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.
Chapter 5: Control Structures II (Repetition)
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
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.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
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.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Introduction to Problem Solving and Control Statements.
Loops and Files. 5.1 The Increment and Decrement Operators.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
7-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code that may be executed several times. Fixed-count (definite) loops repeat a fixed.
© 2006 Lawrenceville Press Slide 1 Chapter 6 The Post-Test Do…Loop Statement  Loop structure that executes a set of statements as long as a condition.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Controlling Program Flow with Looping Structures
Unit 6 Repetition Processing Instructor: Brent Presley.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Iterations (aka Loops). 2 Loops Loops (iterations) are segments of code (loop body) that may be executed several times. Fixed-count (definite) loops repeat.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
© 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.
© 2010 Lawrenceville Press Slide 1 Chapter 5 The Do…Loop Statement  Loop structure that executes a set of statements as long as a condition is true. 
REPETITION CONTROL STRUCTURE
Repeating Program Instructions
Chapter 5 The Do…Loop Statement
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Prepared By: Deborah Becker
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:

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() for displaying messages InputBox(“Prompt”,“Title Caption”, default) Prompt: onscreen message Title: string that displays in title bar Default: initial (optional) string value Returns value or string entered into input box. Stores return value or string into a variable.

Chapter 53 InputBox Example Click OK (or press Enter) to accept value and return it as a string to the function call. Click Cancel to return an empty string to the function call. Use assignment statement to store result.

Chapter 54 Potential Error No problem: If user types a number to return value to numeric variable. If user types a string to return string to string variable. If user clicks Cancel to return an empty string to a string variable. Problem: If user clicks Cancel to return an empty string to a numeric variable.

Chapter 55 Resulting Error Program can’t convert a zero-length string to a numeric data type, such as an integer.

Chapter 56 Problem Elimination Convert the results of the InputBox function to a value first.

Chapter 57 List Box Control (lst) Presents a list of choices. Enables user to select from the list. Highlights selected item(s). Occupies a specific amount of screen space. Prevents user from entering data. Displays scrollbar if not all items are displayed within designated control size.

Chapter 58 User gets to list box by tabbing to it, or clicking an item with the mouse, or using the access key and then pressing an arrow.

Chapter 59 List Box Design-Time Properties SelectionModeOne (Default) MultiSimple MultiExtended SortedFalse: Displays in original order True: Arranges in ascending order ItemsItems to display within list Create as property or within code (see next slide)

Chapter 510 Items Display in Order Entered

Chapter 511 SelectedIndex Property Stores selected item’s index value. The first item has an index value of 0. The second item has an index value of 1. If no item is selected, the list box SelectedIndex property is -1. If lstBeverages.SelectedIndex <> -1 Then strBeverage = lstBeverages.Items(lstBeverages.SelectedIndex) End If

Chapter 512 SelectedItem Stores index number of selected item in variable. Displays error message if nothing is selected.

Chapter 513 Default Selected Item Default No item selected Set default item ListBoxname.SelectedItem = Number

Chapter 514 Save Selected Item to Variable SelectedItem Property strItem = lstBeverages.SelectedItem

Chapter 515 Sorted Property Sorts text in order (by Unicode): AA Aa aA aa Requires leading zeros to properly sort numbers. 15 sorts before 5 05 sorts before 15

Chapter 516 Practice Example 1 Create list box named lstBeverages. Use Items property to enter items: Pepsi Diet Pepsi Wild Cherry Pepsi Pepsi One Run program and note order. Set Sorted to True & run program again.

Chapter 517 Adding Items Method Add Items (use to populate list box instead of using Items property or to add item stored in variable to the list) lstBeverages.Items.Add("Vanilla Coke") Adds item to end of list (no index specified) Insert Items in particular location lstBeverages.Items.Insert(“7-Up", 0) Adds item to beginning of list Rest of items move down 1 Specify Sort property (True) to automatically arrange added items.

Chapter 518 Remove Items ListBox.Items.Remove(Item) lstBeverages.Items.Remove(“7-Up”) Removes specific item. lstBeverages.Items.RemoveAt(4) Removes item with 4 th index number. ListBox.Items.Clear() Erases all items in the Items property.

Chapter 519 Structures Sequential Conditional (If, Select Case) Repetition (loops)

Chapter 520 Loop Loop—Structure that executes a group of statements repeatedly. The structure controls number of loops. Looping—Process of repeating a series of instructions. Iteration—A single execution of the statements in the loop. Infinite Loop—A loop that executes forever (bad programming—you need a way to get out of the loop); if happens, press Ctrl+Break. Types: Do While Do Until For…Next

Chapter 521 Do…Loop Process Executes the statement block repeatedly until a specified condition occurs. Useful When programmer does not know how many times the statements need to be executed. Variations Do While Loop (pretest) Do Loop While (posttest)

Chapter 522 Do While…Loop Syntax Do While condition statement block Loop Do marks top of loop. While is the termination condition: at top of the loop. Condition is a logical expression. Loop causes execution to go back to the top of the loop—at the Do statement.

Chapter 523 Do While Pretest Do While Loop Complete False Loop Condition Statements In Loop True Do While condition statement block Loop

Chapter 524 Do While Process Tests condition (represented by diamond in flowchart), thus name conditionally executed. If condition is true, process statement block. Then go back to test the condition again. If condition is false, skip statement block and go to the code after the Loop statement. Note: May never process if condition is false to start. We’ll look at setting up situation before entering loop.

Chapter 525 Example X VariableIterationMessage Box (before loop)N/A

Chapter 526 Loop Body Considerations One or more statements must eventually cause the condition to become false (to prevent infinite loop). The order of statements is critical—the statements affect the result of the loop. Previous example first message: 1 Reverse 2 statements X += 1 MessageBox.Show Reverse statements first message: 0 The loop can terminate immediately if condition is false—make sure loop does not contain code that is needed regardless of condition.

Chapter 527 Counter Variable Variable that regularly increments or decrements during each loop iteration. Increments Add 1 (or another value) to its value X = X + 1orX += 1 Decrements Subtract 1 (or another value) to its value X = X – 1orX -= 1 Proper initialization is critical. Without it, the loop may iterate 1 time too many or 1 time less than it should.

Chapter 528 Breakpoints to See Variable Contents Click Margin Indicator where you want to set the breakpoint. Choose Debug | Step Into. Turn on Autos window.

Chapter 529 Variable Contents Yellow current code Mouse over to see variable contents Click and choose Autos Current X variable value before statement executed

Chapter 530 Current X variable value after statement executed Mouse over to see variable value

Chapter 531 Pretest vs. Posttest Pretest Tests condition at the beginning of the block. If condition is true, complete at least one iteration. If condition is false, no iteration completed. Do While…Loop Posttest Tests condition at the end of the block. Completes at least one iteration. Do…Loop While

Chapter 532 Do…Loop While Process Executes the statement block while the condition is true. Terminates when condition becomes false. Processes at least once (even when false) because the condition is tested after the first iteration. Useful To ensure at least one iteration of the statement block (which is posttest).

Chapter 533 Do…Loop While Syntax Do statement block Loop While condition Do marks top of loop. Loop While tests the condition posttest and causes a new iteration if the condition is true. Condition is a logical expression.

Chapter 534 Do…While Loop Posttest Do Loop Complete False Loop While Condition Statements In Loop True

Chapter 535 Do…Loop While Process Processes statement block first time. Tests condition in the Loop While line. If condition is true, then loops back to Do statement and starts another iteration. If condition is false, then goes to next line of code after the Loop While statement. Note: Always completes one iteration regardless of condition.

Chapter 536 Example 7.5 (from Burrows) Type Randomize() in the Form Load event.

Chapter 537 Accumulator Variable Sum of #s accumulated with each iteration. Accumulator Counter increments

Chapter 538 Let User Control Loop User decides how many times the loop iterates. Program enables user to provide input, which is used for counter. Study code in Tutorial 5-5 (pp ).

Chapter 539 Checkpoint Go through checkpoints on pp

Chapter 540 Do Until…Loop Similar to Do While…Loop except: Different keywords Do Until…Loop condition executes when condition is false, whereas Do While…Loop executes when condition is true. Do Until…Loop exits when condition is true; Do While…Loop exits when condition is false.

Chapter 541 Do Until…Loop Pretest Do Until Loop Complete Loop Condition Statements In Loop False Do Until condition statement block Loop True

Chapter 542 Do Until…Loop Evaluations condition in the Do Until statement. If the condition is false, VB executes statement block. Then evaluates condition again. If the condition is true, VB skips statement block; ends loop.

Chapter 543 Do…Loop Until Similar to Do…Loop While except: Evaluation of condition for True/False is reversed.

Chapter 544 Do…Loop Until Posttest Do Loop Complete True Loop Until Condition Statements In Loop False Ensures one iteration of the statement block. Does statement block until the condition is true; then it exits the loop.

Chapter 545 Do Until Loop Example Create a program to average test scores. Use only labels and buttons. When the user clicks the Average button, the program prompts the user to enter the # of test scores. Use Do Until loop to display Input Boxes to get that number of scores.

Chapter 546 Do…Loop Until Example Create a program that gets a # from the user. Add that number to the same variable (accumulator). Continue looping until the accumulator sum is 10. Display # of #s entered and their sum

Chapter 547 Exercise 7.5 Burrows

Chapter 548 Initialization & Termination Errors often caused because loop iterates 1 too many times or 1 less time than desired. Error stems from the initial value of variable involved in the loop.

Chapter 549 Potential Sources of Problem Initialization is off by one. Loop condition causes problem. See if = corrects error. Statements in loop are out of order.