Download presentation
Presentation is loading. Please wait.
Published byJuniper Williams Modified over 9 years ago
3
for loop while loop do-while loop
4
for (begin point; end point ; incrementation ) { //statements to be repeated }
5
for (c = 0; c < 10 ; c++ ) { cout << “You get no bull from this Kow\n”; }
6
while ( condition ) { //statements to be repeated }
7
int x = 1; while ( x < 10 ) { cout << x << “ “ << x*x << “\n”; x = x +1; }
8
do { //statements to be repeated } while ( condition );
9
float inch = 1.0; do { cout << inch << “ inch = “ << inch * 2.54 << “ cm\n”; inch = inch + 1.0; } while ( inch < 10 );
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.
11
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
12
#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; }
13
#include using std::cin; using std::cout; using std::endl; int main() { char ch, again; int length;
14
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; }
15
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; }
16
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; }
17
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; }
18
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; }
19
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; }
20
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; }
21
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.
22
for loop while loop do-while loop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.