1 / 21

Loops Lesson 4

Loops Lesson 4. Objectives. for loop while loop do-while loop. for loop Syntax. for (begin point; end point ; incrementation ) { //statements to be repeated }. for loop Example. for (c = 0; c &lt; 10 ; c++ ) { cout &lt;&lt; “You get no bull from this Kow <br>”;

ornice
Download Presentation

Loops Lesson 4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. LoopsLesson 4

  2. Objectives • for loop • while loop • do-while loop

  3. for loop Syntax for (begin point; end point ; incrementation ) { //statements to be repeated }

  4. for loop Example for (c = 0; c < 10 ; c++ ) { cout << “You get no bull from this Kow\n”; }

  5. while loop Syntax while ( condition ) { //statements to be repeated }

  6. while loop Example int x = 1; while ( x < 10 ) { cout << x << “ “ << x*x << “\n”; x = x +1; }

  7. do - while loop Syntax do { //statements to be repeated } while ( condition );

  8. do - while loop Example float inch = 1.0; do { cout << inch << “ inch = “ << inch * 2.54 << “ cm\n”; inch = inch + 1.0; } while ( inch < 10 );

  9. Program with Loops Read in a line of text followed by a return. Count the number of characters in the line. Repeat steps 1 & 2 as many times as the user desires.

  10. again Flowchart Promt for sentence Initialize length to 0 Read char. into ch ch != ‘\n’ Increment length by 1 Read next char. into ch ch Print length of text Prompt and read into again again == ‘y’

  11. Code do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; } #include <iostream> using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length;

  12. Code- Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length;

  13. Nested Loops do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; length = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  14. Priming Read do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; length = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  15. Inner while loop do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  16. Outside while loop do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  17. Repeating the Program do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  18. do while Loop do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  19. cin.ignore ( ) do { cout << "Type sentence & press <enter>." << " I'll tell you length" << endl; lenth = 0; cin.get(ch); // "primes" the loop while (ch != '\n') { length++; cin.get(ch); } cout << endl << "Your sentence is " << length << " characters long.\n" << endl; cout << "More input ?? "; cin >> again; cin.ignore(); } while (again == 'y'); return 0; }

  20. Reason for Different Types of Loops A for loop is generally used when you know how many times you want to repeat a section of code. When you don’t know how many times you want to repeat a section of code but you want to specify a condition in which the code is to be repeated , use a while or a do-while loop. A do-while loop is used when you want the code to be executed at least once. In a do-while, the test is done at the end of the loop In a while loop , the condition is checked at the beginning of the loop. If the condition is false, the body of the loop will never be executed.

  21. Summary • for loop • while loop • do-while loop

More Related