for loop while loop do-while loop
for (begin point; end point ; incrementation ) { //statements to be repeated }
for (c = 0; c < 10 ; c++ ) { cout << “You get no bull from this Kow\n”; }
while ( condition ) { //statements to be repeated }
int x = 1; while ( x < 10 ) { cout << x << “ “ << x*x << “\n”; x = x +1; }
do { //statements to be repeated } while ( condition );
float inch = 1.0; do { cout << inch << “ inch = “ << inch * 2.54 << “ cm\n”; inch = inch + 1.0; } while ( inch < 10 );
1.Read in a line of text followed by a return. 2.Count the number of characters in the line. 3.Repeat steps 1 & 2 as many times as the user desires.
again 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 again == ‘y’ Prompt and read into again
#include using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length; do { cout." << " 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 using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length;
do { cout." << " 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; }
do { cout." << " 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; }
do { cout." << " 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 { cout." << " 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 { cout." << " 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 { cout." << " 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 { cout." << " 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; }
1.A for loop is generally used when you know how many times you want to repeat a section of code. 2.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. 3.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 4.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.
for loop while loop do-while loop