



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An in-depth explanation of the for loop, a programming construct used for count-controlled loops. The author covers the syntax of the for loop, its operation, and examples. Topics include initialization, continuation condition, increment, and combination operators. The document also discusses prefix and postfix increment and decrement.
Typology: Slides
1 / 5
This page cannot be seen from the preview
Don't miss anything!




Larry Caretto Computer Science 106
2
3
while (
4
count = 0 while (count < 3 ) { inFile << x << y; cout << x + y << endl; count = count + 1 }
5
Four times (count = 0, 1, 2, 3) 6
7
double m, v; int n; ifstream inFile( “input.dat” ); inFile >> n; int count = 0; // or = 1 while ( count < n ) // or <= n { inFile >> m >> v; cout << “\nmass = “ << m << “, velocity = “ << v << “, KE = “ << m * v * v / 2; count = count + 1; } (^8)
9
11
double x, A, xOld, tol = 1e-12; cout << “This code finds the square “ << “ root of A; enter A: ”; cin >> A; x = 1; do { xOld = x; x = x / 2 + A / ( 2 * x ); } while ( fabs( x – xold ) > tol * fabs( x ) );
12
19
for ( < initialize loop index >; < continuation condition >; < increment > ) { // Statements in loop that // are executed repeatedly }
20
21
1, 3, 5
0 12 24 22
23
Coventional Combination
count = count % a; count %= a;
count = count / a; count /= a;
count = count * a; count *= a;
count = count – a; count –= a;
count = count + a; count += a;
24
++count; //prefix --count; //prefix
count++; //postfixcount--; //postfix
count += 1; count –= 1;
count = count + 1; count = count – 1;
25
Postfix Prefix
k = m + ++j; r = s / --p;
k = m + j++; r = s / p--;
j = j + 1; k = m + j; p = p – 1; r = s / p;
k = m + j; j = j + 1; r = s / p; p = p – 1;
27
for ( int i = 0; i < N; i++ ) for ( index = first; index <= last; index += 3 ) for ( int count = highest; count > 0; count-- ) for ( int current = 20; current <= 80; current += 10 )
28
29