Clearly Visual Basic Programming with Microsoft Visual Basic
Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)
Chapter Objectives After studying Chapter 12, you should be able to: • Write a looping condition and its opposing loop exit condition • Show a pretest loop in both pseudocode and a flowchart • Write a pretest loop using the Do…Loop statement • Utilize counter and accumulator variables • Refresh the screen • Delay program execution • Display a dialog box using the Input. Box function • Abbreviate assignment statements using the arithmetic assignment operators Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 2
Over and Over Again • Repetition structure (loop) – Used to repeatedly process one or more program instructions – The loop’s condition determines if and for how long the instructions are repeated • The requirement for repeating the instructions is referred to as the looping condition because it indicates when the computer should continue “looping” through the instructions • The requirement for not repeating the instructions is referred to as the loop exit condition because it tells the computer when to exit (or stop) the loop Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 3
Over and Over Again A repetition structure can be either a pretest loop or a posttest loop Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 4
Over and Over Again (Cont. ) Figure 12 -1 Examples of looping and loop exit conditions Pretest loop Loop condition is evaluated before the instructions within the loop are processed Posttest loop Evaluation occurs after the instructions within the loop are processed Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 5
Over and Over Again Depending on the result of the evaluation, the instructions in a pretest loop may never be processed The instructions in a posttest loop, however, will always be processed at least once Of the two types of loops the pretest loop is the most commonly used You will learn about pretest loops in this chapter and in chapter 14. Postttest loops are covered in chapter 13 Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 6
Over and Over Again (Cont. ) • Loop Body is the statements between the repeat and end-repeat clauses Figure 12 -2 A problem that requires the sequence and selection structures Figure 12 -3 A problem that requires the sequence and repetition structures Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 7
Over and Over Again (Cont. ) Figure 12 -4 Another problem that requires the sequence and selection structures Figure 12 -5 A problem that requires the sequence, selection, and repetition structures Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 8
Over and Over Again (Cont. ) Figure 12 -6 Two versions of the modified algorithm from Figure 12 -5 Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 9
The Do…Loop Statement • Use the Do…Loop statement to code both pretest and posttest loops • Statements begin with the Do clause and end with the Loop clause • Between both clauses, instructions the computer is to repeat are entered • Referred to as the loop body Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 10
The Do…Loop Statement (Cont. ) Figure 12 -7 Syntax and examples of the Do…Loop statement for a pretest loop Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 11
Counter Variables • Counter • Used to count something • Always numeric variables • Initializing counter variables • Typically assigned a beginning value of either 0 or 1, depending on the value required by the application • Updating • Adding a number to the value stored in the counter variable is called incrementing • Subtracting a number from the value stored in the counter variable is called decrementing Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 12
Counter Variables (Cont. ) Figure 12 -8 Code and processing steps for Example 1 in Figure 12 -7 Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 13
Counter Variables (Cont. ) Repetition structures use counters to count such things as the number of employees paid in a week or the number of positive numbers entered by the user, for the purpose of counting we use integer variables to count In page 268 the integer variable int. Num is referred to as a counter (work the next exercise) Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 14
The Do…Loop Statement Exercise 1 Write a statement or a set of statements to accomplish each of the following a) Sum the odd integers between 1 and 15 using a Do While…Loop repetition statement. Use integer variables sum and count b) Sum the squares of the even integers between 1 and 15 using a Do While…Loop repetition statement. Use integer variables sum and count and initialize them to 0 and 2, respectively c) Display the numbers from 5 to 1 in txt. Result using a Do Until and Integer counter variable counter. Index. Initialize counter. Index to 5 d) Repeat exercise in part c using a Do While…Loop Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 15
The Do…Loop Statement Exercise 2 Write a Visual Basic statement to accomplish each of the following tasks a) Declare variables sum and number to be of type integer b) Assign 1 to variable number c) Assign 0 to the variable sum d) Total variables number and sum and assign the result to variable sum e) Display “The sum is: “ followed by the value of the variable sum in the lbl. Result Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 16
The Do…Loop Statement Exercise 3 Combine the statements that you wrote in the previous exercise into an application that calculates and displays the sum o f integers from 1 to 10. Use a Do While… loop to loop through the calculations and increment statements. The loop should terminate when the value of control variable number becomes 11. Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 17
The Do…Loop Statement Exercise 4 Identify and correct the error(s) in each of the following (you may need to add code): a) The following loop should total the values from 1 to 50. Assume that value is 50 Do While value >= 0 Sum += value Loop b) The following code should display the squares of 1 to 10 in list. Box. Result Dim number As Integer = 1 Do While number < 10 list. Box. Result. Items. Add(number ^ 2) End While Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 18
The Do…Loop Statement c) This segment should display the integers from 888 to 1000 in List. Box. Result. Initialize variable value to 888 Dim value As Integer = 888 Do While value < = 1000 Value -= 1 loop Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 19
Counter Variables (Cont. ) Figure 12 -9 Example of a partial game program that uses a counter Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 20
Counter Variables (Cont. ) • The interface for the Cheerleader application uses a repetition structure to make the “Go Team!” message blink several times when the user clicks the Click Here button Figure 12 -10 Interface for the Cheerleader application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 21
Counter Variables (Cont. ) • Refresh method • • Ensures computer processes previous lines of code that affect the form’s appearance Syntax: Me. Refresh() • Sleep method • • Used to delay program execution Syntax: System. Threading. Thread. Sleep(milliseconds) Figure 12 -11 Algorithm (pseudocode and flowchart) for the Click Here button (Continues) Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 22
Counter Variables (Cont. ) Figure 12 -12 btn. Click. Here_Click procedure in the Cheerleader application Figure 12 -11 Algorithm (pseudocode and flowchart) for the Click Here button Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 23
My Dream Car Application • The interface for the My Dream Car application uses a repetition structure to drag the car image from the left edge of the form to the center of the form • The car image is contained in the pic. Car control, whose Visible property is set to False in the Properties window Figure 12 -13 Interface for the My Dream Car application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 24
My Dream Car Application (Cont. ) Figure 12 -14 Algorithm for the Show the Car button’s Click event procedure Figure 12 -15 btn. Show_Click procedure in the My Dream Car application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 25
The Sales Express Application • Accumulator variable • Numeric variable used for accumulating something • Assigned a value outside the loop • Updated within the loop • Incremented by an amount that varies Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 26
The Sales Express Application (Cont. ) • Priming read • Used to prepare (prime) the loop • Initializes the loop condition by providing its first value • Update read • Allows user to update the value of the input item • Infinite (endless) loop • Loop that has no way to end • To force an infinite loop to end • Click Debug on the menu bar, then Stop Debugging Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 27
The Sales Express Application (Cont. ) Figure 12 -17 Interface for the Sales Express application Figure 12 -16 Problem specification and algorithm for the Sales Express application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 28
The Sales Express Application (Cont. ) • Rather than using a text box, the application will use an input dialog box • An Input. Box function returns a value depending on the button the user chooses • If the user clicks the OK button, the function returns the value contained in the input area of the dialog box; the return value is always treated as a string • 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 string • The empty string is represented by the String. Empty constant Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 29
The Sales Express Application (Cont. ) Figure 12 -18 Example of an input dialog box created by the Input. Box function Figure 12 -19 Syntax and examples of the Input. Box function Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 30
The Sales Express Application (Cont. ) Figure 12 -20 btn. Calc_Click procedure in the Sales Express application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 31
The Sales Express Application (Cont. ) Figure 12 -21 Test data chart for the Sales Express application Figure 12 -22 Sample run of the Sales Express application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 32
Can I Abbreviate That Assignment Statement? • Use the arithmetic assignment operators to abbreviate an assignment statement that contains an arithmetic operator Figure 12 -23 Syntax and examples of the arithmetic assignment operators Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 33
Summary • Use a repetition structure (loop) to repeatedly process one or more program instructions • Repetition structures can be either a pretest loop or a posttest loop - Pretest loops are more commonly used • Use the Do…Loop statement to code a pretest loop – The While keyword in the statement’s condition indicates that the loop instructions should be processed while (as long as) the condition is true – The Until keyword in the statement’s condition indicates that the loop instructions should be processed until the condition becomes true Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 34
Summary (Cont. ) • Counters and accumulators must be initialized and updated – The initialization is done outside of the loop that uses the counter or accumulator, and the updating is done within the loop – Counters are updated by a constant amount, whereas accumulators are usually updated by an amount that varies • In a flowchart, the loop condition is represented by the decision symbol, which is a diamond Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 35
Summary (Cont. ) • Use the Refresh method to refresh (redraw) the form – The method’s syntax is Me. Refresh() • Use the Sleep method to delay program execution – The method’s syntax is System. Threading. Thread. Sleep(milliseconds) • The priming read appears above the loop that it controls • The Input. Box function displays an input dialog box that contains a message, an OK button, a Cancel button, and an input area Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 36
Summary (Cont. ) • A run time error occurs, you can stop the application by clicking DEBUG on the menu bar and then clicking Stop Debugging • Before using a variable as the divisor in an expression, verify that the variable does not contain the number 0 – Dividing by 0 is mathematically impossible and will cause a run time error to occur • Use an arithmetic assignment operator to abbreviate an assignment statement that has the following format, in which variable. Name is the name of the same variable: – variable. Name = variable. Name arithmetic. Operator value Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 37
- Slides: 37