Chapter 12: How Long Can This Go On?

Slides:



Advertisements
Similar presentations
Chapter 6: The Repetition Structure
Advertisements

Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Introduction to Flowcharting
Introduction to Flowcharting A Supplement to Starting Out with C++, 4th Edition by Tony Gaddis Published by Addison-Wesley.
CS0004: Introduction to Programming Repetition – Do Loops.
Programming Logic and Design, Third Edition Comprehensive
Microsoft Visual Basic: Reloaded Chapter Six Repeating Program Instructions.
Repeating Actions While and For Loops
An Introduction to Programming with C++ Fifth Edition
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved. 1 9 Car Payment Calculator Application Introducing the Do While...Loop and Do Until...Loop.
CHAPTER SIX Loop Structures.
CHAPTER 6 Loop Structures.
CHAPTER SIX.
Visual Basic Chapter 1 Mr. Wangler.
REPETITION STRUCTURES. Topics Introduction to Repetition Structures The while Loop: a Condition- Controlled Loop The for Loop: a Count-Controlled Loop.
Chapter 4: The Selection Structure
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Six Repeating Program Instructions.
Lecture Set 5 Control Structures Part D - Repetition with 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.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Seven More on the Repetition Structure.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Visual Basic Programming
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 4 Looping.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Class Average Application Introducing the Do...Loop While and Do...Loop Until.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter Three Memory Locations and Calculations.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 5 Repetition Structures.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
Programming with Microsoft Visual Basic 2012 Chapter 9: Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Chapter 13 Do It, Then Ask Permission (Posttest Loops) Clearly Visual Basic: Programming with Visual Basic nd Edition.
Controlling Program Flow with Looping Structures
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 14 Do It, Then Ask Permission.
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()
Chapter 7: The Repetition Structure Introduction to Programming with C++ Fourth Edition.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Loop Blocks Chapter 6 Part A. Program Blocks 1.Actions- commands, messages, methods 2.Branches- decisions to be made 3.Loops- actions to be repeated.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
REPETITION CONTROL STRUCTURE
Chapter 1: An Introduction to Visual Basic 2015
Lists, Loops, Validation, and More
Chapter 4: The Selection Structure
Repeating Program Instructions
Chapter 6 – Repetition 6.1 Do Loops 6.2 For...Next Loops
Microsoft Visual Basic 2005: Reloaded Second Edition
CIS 16 Application Development Programming with Visual Basic
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
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 12-10 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 12-11 Algorithm (pseudocode and flowchart) for the Click Here button (Continues) Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

Counter Variables (Cont.) Figure 12-12 btnClickHere_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

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 12-13 Interface for the My Dream Car application Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

My Dream Car Application (Cont.) Figure 12-14 Algorithm for the Show the Car button’s Click event procedure Figure 12-15 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 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

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 12-18 Example of an input dialog box created by the InputBox function Figure 12-19 Syntax and examples of the InputBox function Clearly Visual Basic: Programming with Microsoft Visual Basic 2012

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

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

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

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