Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383 CSC530 Data Structures - LOOP 7/9/20161.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Loops.
Advertisements

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
 Wednesday, 9/18/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/18/02  QUESTIONS?? HW #1 due today at 5!!  Today: Loops, and two new data types.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
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.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Control Structures Repetition or Iteration or Looping Part II.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
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.
Loop - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 1.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 12/11/20151.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC141 Computer Science I 2/4/20161.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
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.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Control Structures Repetition or Iteration or Looping Part II.
Basic concepts of C++ Presented by Prof. Satyajit De
REPETITION CONTROL STRUCTURE
while Repetition Structure
CSC141 Computer Science I Zhen Jiang Dept. of Computer Science
CS161 Introduction to Computer Science
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Programming Fundamentals
CSC115 Introduction to Computer Programming
Lecture 4B More Repetition Richard Gesick
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
CSC240 Computer Science III
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Loop Development Zhen Jiang Dept. of Computer Science
Building Java Programs
CSC115 Introduction to Computer Programming
Alternate Version of STARTING OUT WITH C++ 4th Edition
Loops A loop is a repetition control structure.
Computing Fundamentals
CS150 Introduction to Computer Science 1
CSC115 Introduction to Computer Programming
Let’s all Repeat Together
Alternate Version of STARTING OUT WITH C++ 4th Edition
Repetition Statements (Loops) - 2
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Presentation transcript:

Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161

Price is right. Sample execution (click on this link to try)this link Each button in the above sample REPEAT …? Loop 7/9/20162

While loop Format & Logic 7/9/20163

4 ; while ( ) { ; }

Do-while loop Format Logic 7/9/20165

6 How does this differ from the while loop? The controlled will always execute the first time, regardless of whether the is true or false. 7/9/2016

For loop Format Logic 7/9/20167

8 for ( ; ; ) { ; } 7/9/2016

9 Summary Body first, and then event change/update

Development process 7/9/201610

7/9/201611

Controlling Number of Loop Iterations If the number of iterations is known before the loop starts, the loop is called a count- controlled loop. Counter =0, counter++, counter <number Counter = 1, counter++, counter <=number Use for loop for an easy development. 7/9/201612

7/9/201613

7/9/201614

15 Code: for (int i = 1; i <= 4; i++) { cout << i << " squared is " << (i * i) << endl; } Output: 1 squared is 1 2 squared is 4 3 squared is 9 4 squared is 16 7/9/2016

16 Code: cin >> n; // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << “*”; } cout <<endl; } Output: ****** 7/9/2016

17 Code: cin >> n; // try 5! for (int i = 1; i <= n; i++) { for (int j = 1; j <= 10; j++) { cout <<(i * j) << " "; } cout << endl; } Output: /9/2016

18 Code: cin >> n; // try 6! for (i = 1; i<=n; i++) cout << “*”; cout <<endl; for (i = 1; i <= n-2; i++) { cout << “*”; for (int j = 1; j <= n-2; j++) cout <<“ ”; cout <<“*” <<endl; } for (i = 1; i<=n; i++) cout <<“*”; cout << endl; Output: ****** * ****** 7/9/2016

19 Code: cin >> n; // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << "*"; } cout <<endl; } Output: * ** *** **** ***** ****** 7/9/2016

20 Code: cin >> n; // try 6! for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { cout << i; } cout << endl; } Output: /9/2016

21 Code: cin >> n; // try 5! for (int i = 1; i <= n; i++) { for (int j = 1; j <= (n - i); j++) { cout << " "; } for (int k = 1; k <= i; k++) { cout << i; } cout << endl; } Output: /9/2016

Otherwise (unknown or unclear), the loop is called a event-controlled loop. Use a while loop or a do-while loop for an easy checkpoint development. Asking the user before each iteration if it is time to end the loop is called the ask-before-iterating technique. Appropriate status update (or event initializing) for a sequence of iterations 7/9/ Controlling Event of Loop Iterations

7/9/201623

24 Finds and prints a number's first factor other than 1: cin >> n; // try 91 int f = 2; while (n % f != 0) { f++; } cout << "First factor:“ << f <<endl; Sample run: First factor:7 7/9/2016

25 Write a program that will repeatedly prompt the user to type a number until the user types a non-negative number, then square it. Example log: Type a non-negative integer: -5 Invalid number, try again: -1 Invalid number, try again: -235 Invalid number, try again: -87 Invalid number, try again: squared is 121 7/9/2016

26 cout << "Type a non-negative integer: "; cin >> n; while (n < 0) { cout << "Invalid number, try again: "; cin >> n; } int square = n * n; cout << n << " squared is " << square<<endl; Notice that the number variable had to be declared outside the while loop in order to remain in scope. 7/9/2016

27 Write a class named DigitSum that reads an integer from the user and prints the sum of the digits of that number. You may assume that the number is non-negative. Example: Enter a nonnegative number: prints out 19 (i.e., ) Hint: Use the % operator to extract the last digit of a number. If we do this repeatedly, when should we stop? 7/9/2016

28 cin >> n; int sum = 0; while (n > 0) { sum += n % 10; // add last digit to sum n = n / 10; // remove last digit } cout << “sum = “ << sum << endl; 7/9/2016

29 Write a program named CountFactors that reads in an integer and displays its number of factors. For example, if the user enters 60, CountFactors displays 12 because 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60 are all factors of 60. cin >> n; int sum = 0, k = ?; while ( ) { } cout << “sum = “ << sum <<endl; 7/9/2016

Exercise population TV purchase /9/201630

7/9/201631

Solution 7/9/201632