Download presentation
Presentation is loading. Please wait.
Published byBrendan Malone Modified over 9 years ago
1
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
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
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
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
5
Visual C++ Programming 5
6
6
7
7
8
8
9
9
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
11
Visual C++ Programming 11
12
Visual C++ Programming 12
13
Visual C++ Programming 13
14
Visual C++ Programming 14
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
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
17
Visual C++ Programming 17
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
19
Visual C++ Programming 19
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
21
Visual C++ Programming 21
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
23
Visual C++ Programming 23
24
Visual C++ Programming 24
25
Visual C++ Programming 25
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--;
27
Visual C++ Programming 27
28
Visual C++ Programming 28
29
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
30
Visual C++ Programming 30
31
Visual C++ Programming 31
32
Visual C++ Programming 32
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
34
Visual C++ Programming 34
35
Visual C++ Programming 35
36
Visual C++ Programming 36
37
Visual C++ Programming 37
38
Visual C++ Programming 38
39
Visual C++ Programming 39
40
Visual C++ Programming 40
41
Visual C++ Programming 41
42
Visual C++ Programming 42
43
Visual C++ Programming 43
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
45
Formula Translation Visual C++ Programming 45 Summation Initial value Terminal value Summation process Can easily be translated from mathematical symbols into C++ code
46
Visual C++ Programming 46
47
Visual C++ Programming 47
48
Visual C++ Programming 48
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
50
Visual C++ Programming 50
51
Visual C++ Programming 51
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)
53
Visual C++ Programming 53
54
Visual C++ Programming 54
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
56
Visual C++ Programming 56
57
Visual C++ Programming 57
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
59
Visual C++ Programming 59
60
Visual C++ Programming 60
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
62
Visual C++ Programming 62
63
Visual C++ Programming 63
64
Visual C++ Programming 64
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)
66
Visual C++ Programming 66
67
Visual C++ Programming 67
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
69
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
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.