Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 9/29/06CS150 Introduction to Computer Science 1 Loops Section 5.2 - 5.5 Page 255."— Presentation transcript:

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

2 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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


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

Similar presentations


Ads by Google