220 likes | 230 Views
CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++. Prof. Amr Goneid AUC Part 4. Repetition Constructs. Repetition Constructs. Repetition Constructs. The for construct The while construct The do-while construct Various Types of Loops. 1. The for Construct. Syntax:
E N D
CSCE 110PROGRAMMINGFUNDAMENTALSWITH C++ Prof. Amr Goneid AUC Part 4. Repetition Constructs Prof. amr Goneid, AUC
Repetition Constructs Prof. amr Goneid, AUC
Repetition Constructs • The for construct • The while construct • The do-while construct • Various Types of Loops Prof. amr Goneid, AUC
1. The for Construct • Syntax: ei = initialization expression, c = loop repetition condition, eu = update expression, s = statement for (ei ; c ; eu) s; e.g. for ( k = 1; k <= max; k++) cout << k << k*k << endl; Prof. amr Goneid, AUC
Flowchart of a for loop 1. Initialization Expression True False 2. Loop Condition 3. Execute Statement 4. Update Expression Prof. amr Goneid, AUC
Example: #include <iostream.h> void main() { int i, year; float balance, interestRate = 0.05; // 5% cout << ”Enter your deposit: $” cin >> balance; cout << ”Enter the number of years: ” cin >> year; for(i = 0; i < year; i++) balance *= 1.05; cout << ”Your balance after ” << year << ” years: $” << balance << endl; } Prof. amr Goneid, AUC
2. The while Construct • Syntax: c = condition s = statement while (c) s; • Example: c = 65; while (c <= 90) { cout << char(c); c++; } C F T S Prof. amr Goneid, AUC
Example: #include <iostream.h> void main() { int year; float balance; const float interestRate = 0.05; // 5% cout << ”Savings Account: ” << ”This program calculates your ” << ”balance after N years. ” << ”The interest rate is 5%.\n\n”; cout << ”Enter your deposit: $” cin >> balance; cout << ”Enter the number of years: ” cin >> year; Prof. amr Goneid, AUC
while (year > 0) { balance *= 1.05; year--; } cout << ”Your balance after ” << year << ” years: $” << balance <<endl; } Sample Dialogue: Savings Account: This program calculates your balance after N years. The interest rate is 5%. Enter your deposit: $1000 Enter the number of years: 50 Your balance after 50 years: $11467.4000 Prof. amr Goneid, AUC
3. The do-while Construct • Syntax: do s while (c); • Example: c = 65; do { cout << char(c); c++ ; } while ( c <= 90 ) ; S T C F Prof. amr Goneid, AUC
Example: #include <iostream.h> int main() { char answer; do { cout << ”Good day\n”; cout << ”Do you want another greeting?\n” << ”Press y for yes, n for no,\n” << ”and then press Enter.\n” } while (answer == ’y’ || answer == ’Y’); cout << ”See you later!\n”; return 0; } Prof. amr Goneid, AUC
4. Various Types of Loops Prof. amr Goneid, AUC
Various Types of Loops • Up-Counting, Down-Counting and Nested for Loops • Counter-Controlled while Loop • Flag-Controlled while Loop • Sentinel-Controlled while Loop • Data-validation do-while Loop Prof. amr Goneid, AUC
Up-counting for Loops • for (i = 0; i <= 10; s[i++] = 0); • for (j = space = dot = 0; j <= max; j++) { if ( s [ j ] == ‘ ‘) space++; if ( s [ j ] == ‘.’) { dot++ ; s [ j ] = ‘ ‘;} } Prof. amr Goneid, AUC
Down-counting and Nested for Loop • for (n = 99; n >= 1; n - = 2) cout << n << “ “ << sqrt(n) << endl ; • for (c = 90; c >= 65; c--) cout << c << “ “ << char(c) << endl; • for (j = 1; j <= 26; j++) { for (k = 1; k <= j; k++) cout << char(64+k); cout << endl; } Prof. amr Goneid, AUC
Using the break Statement • The break statement can be used to exit early from the for loop. • Examples: for ( n = 0; n <= max; n++) if ( s [ n ] == 0 ) break; for ( k = 0; k <= max; k++) { if ( s [ k ] == ‘\0’ ) { length = k; break;} } Prof. amr Goneid, AUC
Counter-Controlledwhile Loop Initialize count; k = 1; y = 1; while (count <= max) while ( k <= n ) { { Do something; y *= 2; increment count; k++ ; } } Prof. amr Goneid, AUC
Flag-Controlled while Loop flag = false; odd = false; While (!flag) { while (!odd) { do something; cin >> k; if event happens, odd = (k % 2 == 1); reset flag to true; } } Prof. amr Goneid, AUC
Sentinel-Controlled while Loop cout << “ Enter Char “; cout << “ ESC to Quit “; input 1st data item; cin >> c ; while (item != sentinel) while ( int(c) != 27 ) { { Do something; cout << toupper(c) ; input next data item; cout << “ Enter Char “; cin >> c ; } } Prof. amr Goneid, AUC
Data-validation do-while Loop do do { { prompt for data item; cout << “Enter a Digit”; input data item; cin >> d; } while(item not valid); } while (! isdigit(d)); Prof. amr Goneid, AUC
Advanced: Infinite Loops • for ( ; ; ) { break at a certain condition } • while (1) { break at a certain condition } • Example: int n , d ; cout << “Enter n : “ ; cin >> n ; for ( ; ; ) { d = n % 2; cout << d; n /= 2; if (n == 0) break; } Prof. amr Goneid, AUC
Advanced: Multiple Initialization and Update • Example: for ( i = 1, s = 0 , p = 1 ; i <= 5 ; i++ , s += 2 , p *= 2 ) ; cout << i << “ “ << s << “ “ << p ; • Output: 6 10 32 Prof. amr Goneid, AUC