210 likes | 326 Views
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 < 10 ; c++ ) { cout << “You get no bull from this Kow <br>”;
E N D
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 < 10 ; c++ ) { cout << “You get no bull from this Kow\n”; }
while loop Syntax while ( condition ) { //statements to be repeated }
while loop Example int x = 1; while ( x < 10 ) { cout << x << “ “ << x*x << “\n”; x = x +1; }
do - while loop Syntax do { //statements to be repeated } while ( condition );
do - while loop Example float inch = 1.0; do { cout << inch << “ inch = “ << inch * 2.54 << “ cm\n”; inch = inch + 1.0; } while ( inch < 10 );
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.
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’
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;
Code- Part 1 #include <iostream> using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length;
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; }
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; }
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; }
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; }
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; }
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; }
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; }
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.
Summary • for loop • while loop • do-while loop