 # Chapter 12: How Long Can This Go On?

## Presentation on theme: "Chapter 12: How Long Can This Go On?"— Presentation transcript:

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 InputBox function Abbreviate assignment statements using the arithmetic assignment operators Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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

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

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

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

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

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

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

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

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

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

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

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 intNum is referred to as a counter (work the next exercise) Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

The Do…Loop Statement Exercise1
Write a statement or a set of statements to accomplish each of the following Sum the odd integers between 1 and 15 using a Do While…Loop repetition statement. Use integer variables sum and count 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 Display the numbers from 5 to 1 in txtResult using a Do Until and Integer counter variable counterIndex. Initialize counterIndex to 5 Repeat exercise in part c using a Do While…Loop Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

The Do…Loop Statement Exercise2
Write a Visual Basic statement to accomplish each of the following tasks Declare variables sum and number to be of type integer Assign 1 to variable number Assign 0 to the variable sum Total variables number and sum and assign the result to variable sum Display “The sum is: “ followed by the value of the variable sum in the lblResult Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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

The Do…Loop Statement Sum += value Loop Exercise 4
Identify and correct the error(s) in each of the following (you may need to add code): The following loop should total the values from 1 to 50. Assume that value is 50 Do While value >= 0 Sum += value Loop The following code should display the squares of 1 to 10 in listBoxResult Dim number As Integer = 1 Do While number < 10 listBoxResult.Items.Add(number ^ 2) End While Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

The Do…Loop Statement c) This segment should display the integers from 888 to 1000 in ListBoxResult. 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

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

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 Interface for the Cheerleader application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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 Algorithm (pseudocode and flowchart) for the Click Here button (Continues) Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

Counter Variables (Cont.)
Figure btnClickHere_Click procedure in the Cheerleader application Figure Algorithm (pseudocode and flowchart) for the Click Here button Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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 picCar control, whose Visible property is set to False in the Properties window Figure Interface for the My Dream Car application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

My Dream Car Application (Cont.)
Figure Algorithm for the Show the Car button’s Click event procedure Figure btnShow_Click procedure in the My Dream Car application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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

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

The Sales Express Application (Cont.)
Figure Interface for the Sales Express application Figure Problem specification and algorithm for the Sales Express application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

The Sales Express Application (Cont.)
Rather than using a text box, the application will use an input dialog box An InputBox 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

The Sales Express Application (Cont.)
Figure Example of an input dialog box created by the InputBox function Figure Syntax and examples of the InputBox function Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

The Sales Express Application (Cont.)
Figure btnCalc_Click procedure in the Sales Express application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

The Sales Express Application (Cont.)
Figure Test data chart for the Sales Express application Figure Sample run of the Sales Express application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

Can I Abbreviate That Assignment Statement?
Use the arithmetic assignment operators to abbreviate an assignment statement that contains an arithmetic operator Figure Syntax and examples of the arithmetic assignment operators Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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

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

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 InputBox 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

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 variableName is the name of the same variable: variableName = variableName arithmeticOperator value Clearly Visual Basic: Programming with Microsoft Visual Basic 2012