Presentation is loading. Please wait.

Presentation is loading. Please wait.

The for Loop Syntax: Same as: for (expression1;condition; expression2)

Similar presentations


Presentation on theme: "The for Loop Syntax: Same as: for (expression1;condition; expression2)"— Presentation transcript:

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

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

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

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

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

6 The do-while Loop Syntax do statement; while (condition); { }

7 do-while Example char letterEntered; do {
cout << “Enter a letter: “; cin >> letterEntered; } while (letterEntered != ‘Z’);

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

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

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


Download ppt "The for Loop Syntax: Same as: for (expression1;condition; expression2)"

Similar presentations


Ads by Google