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

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

CS0007: Introduction to Computer Programming
Chapter 6: The Repetition Structure
Programming with Microsoft Visual Basic 2008 Fourth Edition
Programming with Microsoft Visual Basic th Edition
CS0004: Introduction to Programming Repetition – Do Loops.
Objectives In this chapter, you will learn about:
Repeating Actions While and For Loops
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Java Programming, 3e Concepts and Techniques Chapter 5 Arrays, Loops, and Layout Managers Using External Classes.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
CS150 Introduction to Computer Science 1
Chapter 5: Control Structures II (Repetition)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Chapter 5: Loops and Files.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Chapter 5: Repetition Statements. In this chapter, you will learn about: Basic loop structures while loops Interactive while loops for loops Loop programming.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 6: Repetition  Some additional operators increment and decrement.
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 5: Control Structures II (Repetition)
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.
Chapter 12: How Long Can This Go On?
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Chapter 4: Loops and Files. The Increment and Decrement Operators  There are numerous times where a variable must simply be incremented or decremented.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 6 Looping.
Chapter 5: Control Structures: Iteration Visual Basic.NET Programming: From Problem Analysis to Program Design.
Chapter 6: The Repetition Structure
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
An Introduction to Programming with C++ Sixth Edition Chapter 7 The Repetition Structure.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
JavaScript, Fourth Edition
Loops and Files. 5.1 The Increment and Decrement Operators.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
For…Next Loops, Checked List Boxes, and Combo Boxes Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 Fall 2009ACS-1903 Ch 4 Loops and Files while loop do-while loop for loop Other topics later on …
Chapter 6: Looping. Objectives Learn about the loop structure Create while loops Use shortcut arithmetic operators Create for loops Create do…while loops.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Lesson 7 Iteration Structures. Iteration is the third control structure we will explore. Iteration simply means to do something repeatedly. All iteration.
Chapter 5: Looping. Using the while Loop Loop – A structure that allows repeated execution of a block of statements Loop body – A block of statements.
Fundamentals of PL/SQL part 2 (Basics)
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Programming Logic and Design Fourth Edition, Comprehensive
Review If you want to display a floating-point number in a particular format use The DecimalFormat Class printf A loop is… a control structure that causes.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Learning About the Loop Structure (continued)
Control Structure Senior Lecturer
CIS 16 Application Development Programming with Visual Basic
Chapter 6: Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Presentation transcript:

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