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