visual c++ programming: concepts and projects chapter 5a repetition (concepts)

Download VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)

If you can't read please download the document

Upload: brendan-malone

Post on 13-Dec-2015

228 views

Category:

Documents


1 download

TRANSCRIPT

  • Slide 1

VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts) Slide 2 Objectives Visual C++ Programming 2 Learn the strengths and weaknesses of user- controlled repetition Study the various forms of repetition control structures Learn how pre- and post-test loops work Use for loops, while loops, and do...while loops Slide 3 Objectives (continued) Visual C++ Programming 3 Generate random numbers Learn how the loop process relates to summation, counting, and other common tasks Use a loop to construct an extended string of characters Slide 4 User-Controlled Repetition Visual C++ Programming 4 User-controlled repetition relies on the user to perform repeated actions Entering data into textboxes Clicking buttons The need for user-controlled repetition Consider a program that requires the user to enter 5 numbers into 5 textboxes This program is difficult to revise to accommodate more or fewer numbers Example: revising it to accommodate 15 Slide 5 Visual C++ Programming 5 Slide 6 6 Slide 7 7 Slide 8 8 Slide 9 9 Slide 10 User-Controlled Repetition (continued) Visual C++ Programming 10 The problem with the previous examples were that they were designed to solve only one version of a broader problem Single-purpose solution Works only with one set of input values General purpose solution Works with any amount of input values General averaging program Uses user-controlled repetition Handles any amount of input Very few variables and interface controls No need to change it to accommodate more data Slide 11 Visual C++ Programming 11 Slide 12 Visual C++ Programming 12 Slide 13 Visual C++ Programming 13 Slide 14 Visual C++ Programming 14 Slide 15 Accumulating a Sum and Counting Visual C++ Programming 15 Summation is the process of adding values to arrive at a sum Accumulating a sum A sum can be accumulated one addition at a time in a repetitive process Counting A process in which the value 1 is added to a counter in a repetitive process Slide 16 Instance Variables Visual C++ Programming 16 Variable scope Local scope The variable is declared in an event handler The variable exists only within that event handler Class scope The variable is declared within the Form1 class but outside of all event handlers Every event handler can use it Called instance variables Instance variables are declared Immediately after the line #pragma endregion Outside of all event handlers Slide 17 Visual C++ Programming 17 Slide 18 Instance Variables (continued) Visual C++ Programming 18 Instance variables are required for summation or counting They are initialized to 0 by default when declared Example: sum, count Each time the user clicks the button a value is added to the sum and 1 is added to the count Slide 19 Visual C++ Programming 19 Slide 20 Repetition Control Structures Visual C++ Programming 20 Repetition control structures allow your program to automatically repeat one or more statements (not user- controlled) Commonly called loops Loop body - the statements in a loop Iteration a pass through the loop body Loop condition Boolean expression that must be true for further iteration to continue Loop control variable a variable used in the loop condition Types of loops Pre-test loop loop condition comes before loop body Post-test loop loop condition comes after the loop body Slide 21 Visual C++ Programming 21 Slide 22 The while Loop Visual C++ Programming 22 A pre-test loop Keyword while, followed by a loop condition and then the loop body Repetition of the loop body continues as long as the pre-test evaluates to true Repetition stops when the pre-test evaluates to false. Control transfers to the next statement after the loop Slide 23 Visual C++ Programming 23 Slide 24 Visual C++ Programming 24 Slide 25 Visual C++ Programming 25 Slide 26 The while Loop (continued) Visual C++ Programming 26 Incrementation Adding 1 to a loop control variable Shorthand version: num += 1; Prefix incrementation: ++num; Postfix incrementation: num++; Decrementation Subtracting 1 from a loop control variable Shorthand version: num -= 1; Prefix decrementation: --num; Postfix decrementation: num--; Slide 27 Visual C++ Programming 27 Slide 28 Visual C++ Programming 28 Slide 29 dowhile Loops Visual C++ Programming 29 Post-test loop condition Keyword do at top of loop, followed by loop body, then keyword while followed by the loop condition Repetition of the loop body continues as long as the post-test evaluates to true Repetition stops and control is transferred to the first statement after the loop when the post-test evaluates to false Slide 30 Visual C++ Programming 30 Slide 31 Visual C++ Programming 31 Slide 32 Visual C++ Programming 32 Slide 33 The for Loop Visual C++ Programming 33 Pre-test loop Built-in loop control variable that often serves as a counter Three clauses in the for statement Initialization of loop control variable Pre-test condition Update of loop control variable The initialization clause is executed only once, when the loop is first encountered The update clause is executed at the end of each iteration, prior to the pre-test evaluation Slide 34 Visual C++ Programming 34 Slide 35 Visual C++ Programming 35 Slide 36 Visual C++ Programming 36 Slide 37 Visual C++ Programming 37 Slide 38 Visual C++ Programming 38 Slide 39 Visual C++ Programming 39 Slide 40 Visual C++ Programming 40 Slide 41 Visual C++ Programming 41 Slide 42 Visual C++ Programming 42 Slide 43 Visual C++ Programming 43 Slide 44 Common Loop Tasks Visual C++ Programming 44 Formula translation Accumulating a product Building a string Generating random numbers Finding the largest/smallest value Counting specific values Nested loops Slide 45 Formula Translation Visual C++ Programming 45 Summation Initial value Terminal value Summation process Can easily be translated from mathematical symbols into C++ code Slide 46 Visual C++ Programming 46 Slide 47 Visual C++ Programming 47 Slide 48 Visual C++ Programming 48 Slide 49 Accumulating a Product Visual C++ Programming 49 Like summation only with multiplication Factorial 0! = 1, 1! = 1, n! = n x (n-1)! 5! Is 5 x 4 x 3 x 2 x 1 Initialize the product to 1 outside of the loop The loop multiplies the product by succesive values Slide 50 Visual C++ Programming 50 Slide 51 Visual C++ Programming 51 Slide 52 Building a String Visual C++ Programming 52 Repeat the process of adding a new character to a String variable within the loop body Declare an empty string outside the loop Add a new character to it with each iteration Concatenating characters Uses the concatenation operator + Or the shorthand concatenation operator += Conversion to String type is implicit ( ToString() is not required) Slide 53 Visual C++ Programming 53 Slide 54 Visual C++ Programming 54 Slide 55 Generating Random Numbers Visual C++ Programming 55 A random number Selected from a range of values Each value has same likelihood of selection Requires system-defined random number object (Random) Starting point should be different each time the program runs Use DateTime variable milliseconds since midnight Next method generates an integer within a specified range Two parameters (first, last) The value will be any integer from first up to (but not including, last Slide 56 Visual C++ Programming 56 Slide 57 Visual C++ Programming 57 Slide 58 Finding the Largest Value Visual C++ Programming 58 Use variable to store the largest value Initialize variable to a small value For positive integers initialize large to 0 Loop body Generate new value Compare value in large to new value If value in large < new value then replace it with the new value Slide 59 Visual C++ Programming 59 Slide 60 Visual C++ Programming 60 Slide 61 Counting Specific Values Visual C++ Programming 61 Initialize a counter to 0 before the loop Loop body Produce a new value If the value is countable then increment the counter Slide 62 Visual C++ Programming 62 Slide 63 Visual C++ Programming 63 Slide 64 Visual C++ Programming 64 Slide 65 Nested Loops Visual C++ Programming 65 One loop within the loop body of another Outer loop Inner loop The inner loop executes many times, each time starting anew Example: Producing a table (rows and columns) After completion of the inner loop a \r\n is used (newline character) Slide 66 Visual C++ Programming 66 Slide 67 Visual C++ Programming 67 Slide 68 Summary Visual C++ Programming 68 User-controlled repetition Used in general solutions involving user interaction Repetition control structures Used to automate the repetition process Types of loops Pre-test loops, entrance condition Post-test loops, exit condition Slide 69 Summary (continued) Visual C++ Programming 69 While loop Pre-test Processing continues indefinitely until the pre-test evaluates to false Dowhile loop Post-test Processing continues indefinitely until the post-test evaluates to false For loop Built-in loop control variable Processing is usually counter controlled Update is automatic, at the end of each iteration Slide 70 Summary (continued) Visual C++ Programming 70 Common uses for loops Formula translation - accumulating a sum Accumulating a product Building a string Generating random numbers Finding the largest/smallest value Counting specific values Nested loops