1 9/29/06CS150 Introduction to Computer Science 1 Loops Section 5.2 - 5.5 Page 255.

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
CMPUT 101 Lab # 5 October 22, :00 – 17:00.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Computer Science 1620 Loops.
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.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Starting Out with C++, 3 rd Edition 1 Chapter 5. Looping.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 5: Looping by Tony.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
CS150 Introduction to Computer Science 1
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
Compound Operators 03/07/11. More Operators, First Section 4.4.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
Chapter 4: Looping. Resource: Starting Out with C++, Third Edition, Tony Gaddis 5.1 The Increment and Decrement Operators ++ and -- are operators that.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
111/18/2015CS150 Introduction to Computer Science 1 Announcements  I have not graded your exam yet.
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.
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.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
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.
1 CS161 Introduction to Computer Science Topic #6.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA CSC530 Data Structures - LOOP 7/9/20161.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
CS161 Introduction to Computer Science
for Repetition Structures
Chapter 2 Assignment and Interactive Input
Chapter 5: Loops and Files.
Chapter 5: Looping Starting Out with C++ Early Objects Seventh Edition
Alternate Version of STARTING OUT WITH C++ 4th Edition
Unary Operators ++ and --
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
do/while Selection Structure
Life is Full of Alternatives
HNDIT11034 More Operators.
Life is Full of Alternatives Part 3
Presentation transcript:

1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255

2 9/29/06CS150 Introduction to Computer Science 1 Review Exercise  Write a program segment that allows the user to input two integer values into variables num1 and num2. Your program is to then exchange the values in the variables num1 and num2 only if num1 is greater than num2

3 9/29/06CS150 Introduction to Computer Science 1 Loop!  So far, we can o Get input ס Produce output o Calculate ס Conditionally execute statements  Loops o Perform the same bit of code many times o Why might we want to do this? statement1; statement2;... statement3;

4 9/29/06CS150 Introduction to Computer Science 1 While Loop  while the expression is true, loop! while( expression ) { statement1; statement2;... statement3; } statement4; Test the expression Either Perform the statements in the loop and return to the while() test or Move past the loop

5 9/29/06CS150 Introduction to Computer Science 1 Example: What happens? int number = 0; while( number < 5 ) { cout << “Number : “ ; cout << number << endl; cout << “Please enter a number : ”; cin >> number; } cout << “The final number is: “; cout << number << endl;

6 9/29/06CS150 Introduction to Computer Science 1 Example: What happens? int number = 0; while( number < 5 && number != 3 ) { cout << “Number : “ ; cout << number << endl; cout << “Please enter a number : ”; cin >> number; } cout << “The final number is: “; cout << number << endl;

7 9/29/06CS150 Introduction to Computer Science 1 Counters (5.3)  Counter: A variable that is incremented or decremented each time a loop runs int theCounter = 0; // initialize the counter while( theCounter < 2 ) // test the counter { cout << “theCounter : “ ; cout << theCounter << endl; theCounter += 1; // increment the counter }  What will happen?

8 9/29/06CS150 Introduction to Computer Science 1 Key Ingredients of while loops  Initialize MUST initialize the counter  Test The value of the counter is tested before each iteration  Update (Increment/Decrement) The counter is changed during each loop iteration If any one of these is missing or incorrect, your loop won’t run properly--not at all, too many/few times or infinitely.

9 9/29/06CS150 Introduction to Computer Science 1 Counters  What will happen? //notice theCounter is now initialized to 1 int theCounter = 1; // initialize the counter while( theCounter < 2 ) // test the counter { cout << “theCounter : “ ; cout << theCounter << endl; theCounter += 1; // increment the counter }

10 9/29/06CS150 Introduction to Computer Science 1 Counters  What will happen? int theCounter = 0; // initialize the counter while( theCounter < 2 ) // test the counter { theCounter += 1; // increment the counter cout << “theCounter : “ ; cout << theCounter << endl; }

11 9/29/06CS150 Introduction to Computer Science 1 Counters  What will happen? int theCounter = 0; // initialize the counter while( theCounter > 2 ) // test the counter { cout << “theCounter : “ ; cout << theCounter << endl; theCounter += 1; // increment the counter }

12 9/29/06CS150 Introduction to Computer Science 1 Practice  Write a snippet of code that will print all the numbers from 0 to 10000

13 9/29/06CS150 Introduction to Computer Science 1 Let the user control the Loop (5.4)  Let the user determine how many times to run the loop int theCounter = 0; // initialize the counter int maxValue; cout << “How many times should we run the loop? “; cin >> maxValue; while( ) // test the counter { cout << “theCounter : “ ; cout << theCounter << endl; // increment the counter }

14 9/29/06CS150 Introduction to Computer Science 1 Practice  Write a snippet of code that will ask the user for a number. Print the numbers from 0 to the square of the number the user supplied.

15 9/29/06CS150 Introduction to Computer Science 1 Running totals (5.5)  How many hours did you work on assignment 1? // initialize the counter int days; // let the user tell us how many times to loop cout << “How many days did you work on assignment 1? “; cin >> days; while( ) // test the counter { cout << “How many hours did you work on day “ << << “? ”; // increment the counter }

16 9/29/06CS150 Introduction to Computer Science 1 Practice  Write a snippet of code that will ask the user for a number. Print the sum of all the numbers from 0 to the number the user supplied.

17 9/29/06CS150 Introduction to Computer Science 1 Practice  Write a snippet of code that will ask the user for a number. Print the sum of all the even numbers from 0 to the square of the number the user supplied.

18 9/29/06CS150 Introduction to Computer Science 1 Exercise  Write a snippet of code that will ask for a student’s exam score and then print the appropriate letter grade (A,B,C,D,F).  Continue asking for exam scores and printing letter grades until the user enters a negative exam score int examScore;

19 9/29/06CS150 Introduction to Computer Science 1 Generating a Random number #include int num; // seed the random number generator srand(time(0)); // produce a random number num = rand() % 101; // what are the possible values for num?

20 9/29/06CS150 Introduction to Computer Science 1 Increment and Decrement Operators  C++ provides a shortcut to increment or decrement a variable by 1 int x = 99, y = 90; x++; // this is equivalent to x += 1 x--; // this is equivalent to x -= 1

21 9/29/06CS150 Introduction to Computer Science 1 In a Loop  Often, this is used to increment a loop counter int x = 1; while( x < 100 ){ cout << “ x : “ << x << endl; x++; }

22 9/29/06CS150 Introduction to Computer Science 1 Examples  This can be used in an expression: y = x++ + 9; This is equivalent to: y = x + 9; x += 1;  This can also be used in a conditional ( x-- > 9 ) is equivalent to: ( x > 9); x -= 1;

23 9/29/06CS150 Introduction to Computer Science 1 Practice  Write one statement of code to do the following: int x =0, y=1;  Add x + 9 to y and increment x by 1  Add x * 4 to y and increment x by 1  Add y – 13 to x and decrement y by 1

24 9/29/06CS150 Introduction to Computer Science 1 Prefix vs Postfix  Another way to write x++ (x--) is: ++x (--x) o ++x is prefix  The x += 1 happens before the expression is evaluated o x++ is postfix  the x += 1 happens after the expression is evaluated int y=0, x=0; x = y++ + 1; // x = y + 1; y += 1; y = ++x + 1; // x += 1; y = x + 1;

25 9/29/06CS150 Introduction to Computer Science 1 Example  What is the output if i = 2 ? cout << “Value of i is “ << i; cout << “Value of i++ is “ << i++; cout << “Value of ++i is “ << ++i; cout << “Value of i-- is “ << i--; cout << “Value of --i is “ << --i;

26 9/29/06CS150 Introduction to Computer Science 1 Examples int x = 0, y = 0; x = y++ * 2; y = ++x / 2; x = x++ + 1; x = ++x + 1; y = (y+ x++) * 2; x = y x;

27 9/29/06CS150 Introduction to Computer Science 1 Practice  Write a single C++ statement to do the following: int y = 0, x = 0, z = 0;  Decrement x by 1 then add 2x to y  Add 2y to x then increment y by 1  Subtract 9x – 1 from y then decrement x by 1  Increment y by 1 then add 8-2y to x  Increment x and y each by 1 then add x+y to z