Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go...

download Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go On? (Pretest Loops)

of 37

  • date post

    12-Jan-2016
  • Category

    Documents

  • view

    220
  • download

    0

Embed Size (px)

Transcript of Clearly Visual Basic: Programming with Microsoft Visual Basic 2012 Chapter 12: How Long Can This Go...

Chapter 12: How Long Can This Go On?(Pretest Loops)Clearly Visual Basic:Programming with Microsoft Visual Basic 20121Chapter ObjectivesAfter studying Chapter 12, you should be able to:Write a looping condition and its opposing loop exit conditionShow a pretest loop in both pseudocode and a flowchartWrite a pretest loop using the DoLoop statementUtilize counter and accumulator variablesRefresh the screenDelay program executionDisplay a dialog box using the InputBox functionAbbreviate assignment statements using the arithmetic assignment operatorsClearly Visual Basic: Programming with Microsoft Visual Basic 201222Over and Over AgainClearly Visual Basic: Programming with Microsoft Visual Basic 20123Repetition structure (loop)Used to repeatedly process one or more program instructionsThe loops condition determines if and for how long the instructions are repeatedThe requirement for repeating the instructions is referred to as the looping condition because it indicates when the computer should continue looping through the instructionsThe 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 loop3Over and Over AgainClearly Visual Basic: Programming with Microsoft Visual Basic 20124A repetition structure can be either a pretest loop or a posttest loop4Over and Over Again (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 20125Figure 12-1 Examples of looping and loop exit conditionsPretest loopLoop condition is evaluated before the instructions within the loop are processedPosttest loopEvaluation occurs after the instructions within the loop are processed

5Over and Over AgainClearly Visual Basic: Programming with Microsoft Visual Basic 20126Depending on the result of the evaluation, the instructions in a pretest loop may never be processedThe instructions in a posttest loop, however, will always be processed at least onceOf the two types of loops the pretest loop is the most commonly usedYou will learn about pretest loops in this chapter and in chapter 14. Postttest loops are covered in chapter 136Over and Over Again (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 20127Figure 12-2 A problem that requires the sequence and selection structuresFigure 12-3 A problem that requires the sequence and repetition structures

Loop Body is the statements between the repeat and end-repeat clauses7

Over and Over Again (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 20128Figure 12-4 Another problem that requires the sequence and selection structuresFigure 12-5 A problem that requires the sequence, selection, and repetition structures

8Over and Over Again (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 20129Figure 12-6 Two versions of the modified algorithm from Figure 12-5

9The DoLoop StatementClearly Visual Basic: Programming with Microsoft Visual Basic 201210Use the DoLoop statement to code both pretest and posttest loopsStatements begin with the Do clause and end with the Loop clauseBetween both clauses, instructions the computer is to repeat are enteredReferred to as the loop body10

The DoLoop Statement (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201211Figure 12-7 Syntax and examples of the DoLoop statement for a pretest loop11CounterUsed to count somethingAlways numeric variablesInitializing counter variablesTypically assigned a beginning value of either 0 or 1, depending on the value required by the applicationUpdating Adding a number to the value stored in the counter variable is called incrementingSubtracting a number from the value stored in the counter variable is called decrementing

Counter VariablesClearly Visual Basic: Programming with Microsoft Visual Basic 20121212

Counter Variables (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201213Figure 12-8 Code and processing steps for Example 1 in Figure 12-713Counter Variables (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201214Repetition 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 countIn page 268 the integer variable intNum is referred to as a counter(work the next exercise)14The DoLoop StatementClearly Visual Basic: Programming with Microsoft Visual Basic 201215Exercise1Write a statement or a set of statements to accomplish each of the followingSum the odd integers between 1 and 15 using a Do WhileLoop repetition statement. Use integer variables sum and countSum the squares of the even integers between 1 and 15 using a Do WhileLoop repetition statement. Use integer variables sum and count and initialize them to 0 and 2, respectivelyDisplay the numbers from 5 to 1 in txtResult using a Do Until and Integer counter variable counterIndex. Initialize counterIndex to 5Repeat exercise in part c using a Do WhileLoop

15The DoLoop StatementClearly Visual Basic: Programming with Microsoft Visual Basic 201216Exercise2Write a Visual Basic statement to accomplish each of the following tasksDeclare variables sum and number to be of type integerAssign 1 to variable numberAssign 0 to the variable sumTotal variables number and sum and assign the result to variable sumDisplay The sum is: followed by the value of the variable sum in the lblResult

16The DoLoop StatementClearly Visual Basic: Programming with Microsoft Visual Basic 201217Exercise 3Combine 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.

17The DoLoop StatementClearly Visual Basic: Programming with Microsoft Visual Basic 201218Exercise 4Identify 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 50Do While value >= 0 Sum += value LoopThe following code should display the squares of 1 to 10 in listBoxResultDim number As Integer = 1Do While number < 10listBoxResult.Items.Add(number ^ 2)End While18The DoLoop StatementClearly Visual Basic: Programming with Microsoft Visual Basic 201219c) This segment should display the integers from 888 to 1000 in ListBoxResult. Initialize variable value to 888

Dim value As Integer = 888Do While value < = 1000Value -= 1loop19

Counter Variables (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201220Figure 12-9 Example of a partial game program that uses a counter20Counter Variables (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201221Figure 12-10 Interface for the Cheerleader applicationThe 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

21

Counter Variables (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201222Figure 12-11 Algorithm (pseudocode and flowchart) for the Click Here button (Continues)Refresh methodEnsures computer processes previous lines of code that affect the forms appearanceSyntax: Me.Refresh()Sleep methodUsed to delay program executionSyntax: System.Threading.Thread.Sleep(milliseconds)22

Counter Variables (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201223Figure 12-11 Algorithm (pseudocode and flowchart) for the Click Here button

Figure 12-12 btnClickHere_Click procedure in the Cheerleader application23My Dream Car ApplicationClearly Visual Basic: Programming with Microsoft Visual Basic 201224The 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 formThe car image is contained in the picCar control, whose Visible property is set to False in the Properties windowFigure 12-13 Interface for the My Dream Car application

24My Dream Car Application (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201225Figure 12-15 btnShow_Click procedure in the My Dream Car applicationFigure 12-14 Algorithm for the Show the Car buttons Click event procedure

25The Sales Express ApplicationClearly Visual Basic: Programming with Microsoft Visual Basic 201226Accumulator variable Numeric variable used for accumulating somethingAssigned a value outside the loopUpdated within the loopIncremented by an amount that varies26The Sales Express Application (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201227Priming readUsed to prepare (prime) the loopInitializes the loop condition by providing its first valueUpdate readAllows user to update the value of the input itemInfinite (endless) loopLoop that has no way to endTo force an infinite loop to endClick Debug on the menu bar, then Stop Debugging

27

The Sales Express Application (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201228Figure 12-17 Interface for the Sales Express application

Figure 12-16 Problem specification and algorithm for the Sales Express application28The Sales Express Application (Cont.)Clearly Visual Basic: Programming with Microsoft Visual Basic 201229Rather than using a text box, the application will use an input dialog boxAn InputBox function returns a value depending on the button the user choosesIf 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 stringIf the user clicks either the Cancel button in the dialog box or the Close button on the dialog boxs title bar,