© 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,

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.
Repeating Actions While and For Loops
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.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
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.
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
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.
 What are the different types of loops? ◦ Do….While  Performs statements within loop while a condition is true ◦ Do….Until  Performs statements within.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Copyright © 2014 Pearson Education, Inc. Chapter 5 Lists 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?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Copyright © 2001 by Wiley. All rights reserved. Chapter 5: The Repetition Process in Visual Basic Event Driven Loops Determinate Loops Indeterminate Loops.
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.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 5 Lists, Loops, Validation, and More.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
Chapter 5 Lists, Loops, Validation and More Instructor: Bindra Shrestha University of Houston – Clear Lake CSCI
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)
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
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.
Week 6 Lists, Loops, Validation, and More. 2 Introduction This chapter covers the Visual Basic looping statements Do … While Do … Until For … Next It.
Introduction to Problem Solving and Control Statements.
Chapter 7 - Lists, loops and printing w List boxes and combo boxes several types can add items at design time or during run time user select from predefined.
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
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.
 A ListBox control displays a list of items and allows the user to select one or more  Drag from Toolbox to create this control on a form.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Visual Basic.NET BASICS Lesson 11 List Boxes, For Next Loops, and Label Settings.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
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.
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()
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Microsoft Visual Basic 2008: Reloaded Third Edition
REPETITION CONTROL STRUCTURE
Lists, Loops, Validation, and More
Repeating Program Instructions
Chapter 5 Lists and Loops.
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Tony Gaddis Kip Irvine STARTING OUT WITH Visual Basic 2008
Presentation transcript:

© 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, Loops, Validation, and More

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 2 The Do Until and For Next Loops The Do Until Loop Iterates Until Its Test Expression Is True The For...Next Loop Is Designed to Use a Counter Variable and Iterates a Specific Number of Times

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 3 Do Until: Pretest and Posttest Forms Pretest: Posttest: Do Until expression statement(s) Loop Do statement(s) Loop Until expression

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 4 Pretest Do Until Example input = InputBox("How many test scores do you want " & _ "to average?", "Enter a Value") numScores = Val(input) total = 0 count = 1 Do Until count > numScores input = InputBox("Enter the value for test score " & _ count.ToString, "Test Score Needed") total = total + Val(input) count = count + 1 Loop

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 5 For … Next Loop, I The syntax is Where 'For', 'To', and 'Next' are keywords Other details follow … For CounterVariable = StartValue To EndValue _[Step] statement Next [CounterVariable]

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 6 For … Next Loop, II CounterVariable is a numeric counter variable StartValue is the initial value of the counter EndValue gives the number to test for completion Step indicates the increment for count at the end of each iteration; it is optional and defaults to 1 if not specified

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 7 For…Next Flowchart Counter = EndValue? statement(s) False True set counter to StartValue increment counter

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 8 For…Next Example For count = 1 To 10 square = count ^ 2 str = "The square of " & count.ToString & " is " & _ square.ToString lstOutput.Items.Add(str) Next count

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 9 More on the StepValue It is optional, if not specified, it defaults to 1 It may be negative, in which case the loop counts downwards For x = 0 To 100 Step 10 MessageBox.Show("x is now " & x.ToString) Next x For x = 10 To 1 Step -1 MessageBox.Show("x is now " & x.ToString) Next x

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 10 Exiting a Loop Prematurely In some situations it is convenient to be able to gracefully end a loop early Exit Do (used in Do While or Until loops) Exit For (used in For Next loops) Will accomplish that task Since these override the normal loop termination mechanism, they should be used sparingly

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 11 Exiting a Loop Prematurely, Example maxNumbers = CInt(InputBox("How many numbers do " & _ "you wish to sum?")) total = 0 For x = 1 to maxNumbers input = InputBox("Enter a number.") If input = "" Then Exit For Else num = CDbl(input) total += num End If Next x MessageBox.Show("The sum of the numbers is " & total.ToString)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 12 When to Use the Do While Loop Use the Do While loop when you wish the loop to repeat as long as the test expression is true You can write the Do While loop as a pretest or posttest loop Pretest Do While loops are ideal when you do not want the loop to iterate if the test expression is false from the beginning Posttest loops are ideal when you always want the loop to iterate at least once

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 13 When to Use the Do Until Loop Use the Do Until loop when you wish the loop to repeat until the test expression is true You can write the Do Until loop as a pretest or posttest loop Pretest Do Until loops are ideal when you do not want the loop to iterate if the test expression is true from the beginning Posttest loops are ideal when you always want the loop to iterate at least once

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 14 When to Use the For Next Loop The For...Next loop is a pretest loop that first initializes a counter variable to a starting value It automatically increments the counter variable at the end of each iteration The loop repeats as long as the counter variable is not greater than an end value The For...Next loop is primarily used when the number of required iterations is known

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 15 Nested Loops Nested Loops Are Necessary When a Task Performs a Repetitive Operation and That Task Itself Must Be Repeated

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 16 Nested Loop Example, I For hours = 0 To 24 lblHours.Text = hours.ToString For minutes = 0 To 59 lblMinutes.Text = minutes.ToString For seconds = 0 To 59 lblSeconds.Text = seconds.ToString Next seconds Next minutes Next hours

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 17 Nested Loop Example, II The innermost loop will iterate 60 times for each iteration of the middle loop The middle loop will iterate 60 times for each iteration of the outermost loop When the outermost loop has iterated 24 times, the middle loop will have iterated 1440 times and the innermost loop will have iterated 86,400 times.

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 18 Multicolumn List Boxes, Checked List Boxes and Combo Boxes A Multicolumn List Box Displays Items in Columns With a Horizontal Scroll Bar, If Necessary A Checked List Box Displays a Check Box Next to Each Item in the List A Combo Box Is Like a List Box Combined With a Text Box

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 19 List Box Multicolumn Property When this property is set to true (false is the default), entries can appear side by side Below, ColumnWidth is set to 30

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 20 Checked List Box Items in list boxes can be checked (in addition to being selected) The CheckOnClick property values are  False - the user clicks an item once to select it, again to check it  True - the user clicks an item only once to both select it and check it

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 21 Checking the Status of Checked Items The method CheckedListBox.GetItemChecked(Index) Returns true if the indexed item is checked, otherwise, false

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 22 Combo Boxes Are Much Like List Boxes They both display a list of items to the user They both have Items, Items.Count, SelectedIndex, SelectedItem, and Sorted properties They both have Items.Add, Items.Clear, Items.Remove, and Items.RemoveAt methods All of these properties and methods work the same with combo boxes and list boxes

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 23 Combo Boxes Also Have: An area like a text box The user may enter text into that area Or the user may fill it by selecting text from the choices provided

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 24 Combo Box Styles, I Simple Combo Box Drop-down Combo Box

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 25 Combo Box Styles, II Drop-down List Combo Box Behaves like a Drop-Down Combo Box, but the user may not enter text directly

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 26 Input Validation As Long As the User of an Application Enters Bad Input, the Application Will Produce Bad Output Applications Should Be Written to Filter Out Bad Input

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 27 Examples of Input Validation Numbers are checked to ensure they are within a range of possible values For example, there are 168 hours in a week - It is not possible for a person to work more than 168 hours in one week Values are checked for their “reasonableness” Although it might be possible for a person to work 168 hours in a week, it is not probable Items selected from a menu or other sets of choices are checked to ensure they are available options Variables are checked for values that might cause problems, such as division by zero

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 28 CausesValidation/Validating Event A control’s Validating event is triggered just before the focus is shifted to another control whose CausesValidation property is set to true This allows one's code to validate an entry just before focus shifts (clearly after the user thinks the input is finished)

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 29 Using the With…End Statement This statement establishes a default object Instead of this code Use this code txtNum1.SelectionStart = 0 txtNum1.SelectionLength = txtNum1.Text.Length With txtNum1.SelectionStart = 0.SelectionLength =.Text.Length End With

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 30 Tool Tips Tool Tips Are a Standard and Convenient Way of Providing Help to the Users of an Application Visual Basic.NET Provides the ToolTip Control, Which Allows You to Assign Tool Tips to the Other Controls on a Form

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 31 What is a Tool Tip? Those little text messages that you see when you pause the mouse cursor over a control They are available for use on Visual Basic.NET forms

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 32 Where the Tool Tip Text is Placed Add the ToolTip control to your form It is visible in Design View But is invisible at Run Time Now controls have a new property with the name you gave to the above ToolTip control This new property holds the text string that will be displayed for that control

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 9: Chapter 5: Slide 33 Controlling the Tool Tips The ToolTip control has an InitialDelay property that regulates the delay before a tip appears It also has a AutoPopDelay determines how long a tip is displayed ReshowDelay determines the minimum time between displaying different tips (as the user moves the mouse about the screen)