Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm.

Similar presentations


Presentation on theme: "1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm."— Presentation transcript:

1 1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm

2 2 Exam 1 16 Scores: 91 88 88 88 85 82 76 75 74 73 71 71 67 62 47 43 Avg 73.8 Std dev 14.1

3 3 Syntax  while (condition) statement;  With multiple statement body:  while (condition) { statement; statement;} The while Loop

4 4 Syntax:  for (expression1; condition; expression2) statement;  for (expression1; condition; expression2){  statement;  } Same as:  expression1;while (condition){ statement; expression2;} The for Loop

5 5 Example for Loop int numEmployees,curNum; cout << “Enter Number of Employees: “; cin >> numEmployees; if (numEmployees > 0) { for (curNum = 0; curNum < numEmployees; curNum++) { cout << “Welcome to CorpLand!” << endl; } else { cout << "Egads!! Negative number of employees??" << endl; }

6 6 Looping for Input char letterEntered = ‘A’; while (letterEntered != ‘Z’) { cout << “Enter a letter: “; cin >> letterEntered; }

7 7 Looping for Input char command; cout << "Welcome to our program!!" << endl << "We'll process a sequence of commands" << endl << "Enter command (or Q to quit): "; cin >> command; while (command != 'Q') { //... do stuff for this command... cout << “Enter command (or Q to quit): “; cin >> command; }

8 8 Looping until Flag boolean noMoreData(); boolean done = false; … while (!done) { … // do a bunch of stuff … if (noMoreData() == true) done = true; } cout << “We’re all through – thank you!”

9 9 Nested Loops int i, j; for (i=0; i < 5; i++) { for (j=0; j < 6; j++) { cout << i << j << “ “; } cout << endl; }

10 10 Nested Loops int i, j; for (i=0; i < 5; i++) { for (j=0; j < i; j++) { cout << i << j << “ “; } cout << endl; }

11 11 Syntax  do statement;while (condition);  do{ statement; statement;}while (condition); Do-while Loop

12 12 do-while Example int inputNum = 0; do { //... do something with inputNum... cout << “Please Input Number, –1 to quit: “; cin >> inputNum; } while (inputNum != -1);

13 13 What Is This? while (stringEntered != “apple”) { cout << “Enter a red fruit: “; cin >> stringEntered; } while (stringEntered != “banana”) { cout << “Enter a yellow fruit: “; cin >> stringEntered; } while (stringEntered != “pomegranate”) { cout << “Enter a random fruit: “; cin >> stringEntered; }

14 14 What Is This? for (i = 0; i < 10 ; i++) cout << “Interesting, isn’t it?” << endl; while (answer != ‘D’) { cout << “Enter an answer: “; cin >> answer; } while (answer != ‘D’) { cout << “Enter an answer: “; cin >> answer; }

15 15 What Is This? void kingsChair(int loopCounter) { int num; for(num = 0; num < loopCounter; num++) cout << “AAAAAAAAAAAAAAAAAAAAAAAAAA” << endl; }


Download ppt "1 CS 105 Lecture 7 For & Do-While Loops Sun, Feb 27, 2011, 2:16 pm."

Similar presentations


Ads by Google