VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
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
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
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
Visual C++ Programming 5
6
7
8
9
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
Visual C++ Programming 11
Visual C++ Programming 12
Visual C++ Programming 13
Visual C++ Programming 14
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
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
Visual C++ Programming 17
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
Visual C++ Programming 19
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
Visual C++ Programming 21
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
Visual C++ Programming 23
Visual C++ Programming 24
Visual C++ Programming 25
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--;
Visual C++ Programming 27
Visual C++ Programming 28
do…while 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
Visual C++ Programming 30
Visual C++ Programming 31
Visual C++ Programming 32
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
Visual C++ Programming 34
Visual C++ Programming 35
Visual C++ Programming 36
Visual C++ Programming 37
Visual C++ Programming 38
Visual C++ Programming 39
Visual C++ Programming 40
Visual C++ Programming 41
Visual C++ Programming 42
Visual C++ Programming 43
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
Formula Translation Visual C++ Programming 45 Summation Initial value Terminal value Summation process Can easily be translated from mathematical symbols into C++ code
Visual C++ Programming 46
Visual C++ Programming 47
Visual C++ Programming 48
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
Visual C++ Programming 50
Visual C++ Programming 51
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)
Visual C++ Programming 53
Visual C++ Programming 54
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
Visual C++ Programming 56
Visual C++ Programming 57
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
Visual C++ Programming 59
Visual C++ Programming 60
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
Visual C++ Programming 62
Visual C++ Programming 63
Visual C++ Programming 64
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)
Visual C++ Programming 66
Visual C++ Programming 67
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
Summary (continued) Visual C++ Programming 69 While loop Pre-test Processing continues indefinitely until the pre-test evaluates to false Do…while 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
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