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.
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.
1 11/05/07CS150 Introduction to Computer Science 1 Functions Chapter 6, page 303.
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/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
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.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
The If/Else Statement, Boolean Flags, and Menus Page 180
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
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.
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.
 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.
Previously Repetition Structures While, Do-While, For.
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.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
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.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Control Structures RepetitionorIterationorLooping Part I.
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.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
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 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.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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
while Repetition Structure
CS161 Introduction to Computer Science
for Repetition Structures
Chapter 2 Assignment and Interactive Input
Computer Science 101 While Statement.
Repetition Statements
Alternate Version of STARTING OUT WITH C++ 4th Edition
Unary Operators ++ and --
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Let’s all Repeat Together
do/while Selection Structure
CS150 Introduction to Computer Science 1
Life is Full of Alternatives
Repetition Statements (Loops) - 2
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;