Lecture 7 Repeating a Known Number of Times
Lecture 7: Repeating a Known Number of Times
Repetition Essentials Loop Group of instructions computer executes repeatedly while some condition remains true. Counter-controlled repetition Definite repetition: know how many times loop will execute Control variable used to count repetitions Sentinel-controlled repetition Indefinite repetition Used when number of repetitions not known Sentinel value indicates "end of data"
Counter-Controlled Repetition Counter-controlled repetition requires The name of a control variable (or loop counter) The initial value of the control variable An increment (or decrement) by which the control variable is modified each time through the loop A condition that tests for the final value of the control variable (i. e. , whether looping should continue)
Counter-Controlled Repetition - Example Problem Write a power algorithm (ex. x 5, where x is a user defined number) Formulating algorithm Pseudocode Set product to 1 Type in base-x Set counter to 1 While counter is less than or equal to 5 product = base-x * product add one to the counter Print out product
C code for Computing x 5 /* Computing x^5, where x is a user defined number */ #include <stdio. h> /* function main begins program execution */ int main( void ) { int product = 1; /* product of x^5 */ int counter = 1; /* number of x's multipled */ int base; /* base x */ printf( "Enter the base: n" ); /* prompt */ scanf( "%d", &base ); /* read an integer base */ while ( counter <= 5 ) { product = base * product; counter = counter + 1; } printf("%d^5 equals to %dn", base, product); return 0; /* indicate that program ended successfully */ } /* end function main */ Definition and assignment are performed simultaneously
Improving C code for Computing x 5 /* Computing x^5, where x is a user defined number */ #include <stdio. h> /* function main begins program execution */ int main( void ) { int product = 1; /* product of x^5 */ int counter = 1; /* number of x's multipled */ int base; /* base x */ Control counting loops with integer values. printf( "Enter the base: n" ); /* prompt */ scanf( "%d", &base ); /* read an integer base */ while ( counter++ <= 5 ) { product = base * product; } Using the post-increment operator printf("%d^5 equals to %dn", base, product); return 0; /* indicate that program ended successfully */ } /* end function main */ Can we use the pre-increment operator? How?
for Repetition Statement Can loop a known number of times Format when using for loops for ( initialization; loop. Continuation. Test; increment ) statement; Revisit C code for computing x 5 /* Computing x^5, where x is a user defined number */ Incrementing control variable #include <stdio. h> /* function main begins program execution */ int main( void ) { int product = 1; /* product of x^5 */ int counter = 1; /* number of x's multipled */ int base; /* base x */ printf( "Enter the base: n" ); /* prompt */ scanf( "%d", &base ); /* read an integer base */ while ( counter <= 5 ) { product = base * product; counter = counter + 1; } Condition that tests for the final value of the control variable Initializing the control variable printf("%d^5 equals to %dn", base, product); return 0; /* indicate that program ended successfully */ } /* end function main */
Using for Statement to Compute x 5 /* Computing x^5, where x is a user defined number Using for repetition statement */ #include <stdio. h> /* function main begins program execution */ int main( void ) { int product = 1; /* product of x^5 */ int counter; /* number of x's multipled */ int base; /* base x */ printf( "Enter the base: n" ); /* prompt */ scanf( "%d", &base ); /* read an integer base */ for ( counter = 1; counter <= 5; counter++ ) product = base * product; printf("%d^5 equals to %dn", base, product); return 0; /* indicate that program ended successfully */ } /* end function main */ for statement header for loop body Can loop a known number of times Simpler code than while loop if known number of iterations.
A Close Look of for Statement Header for keyword Control variable name Required semicolon separator Final value of control variable for which the condition is true Required semicolon separator for ( counter = 1; counter <= 5; counter++ ) Intial value of control variable Loop-continuation condition Increment of control variable
Flowcharting a Typical for repetition Statement 5 product = base * product
for Repetition Statement for loops can usually be rewritten as while loops for ( initialization; loop. Continuation. Test; increment ) statement; initialization; while ( loop. Continuation. Test ) { statement; increment; } Initialization and increment Can be comma-separated lists Example: for (i = 0, j = 0; j + i <= 10; j++, i++) printf( “%dn”, j+i );
for Repetition Statement Arithmetic expressions Initialization, loop-continuation, and increment can contain arithmetic expressions. If x equals 2 and y equals 10 for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) Notes about the for statement: "Increment" may be negative (decrement) If the loop continuation condition is initially false The body of the for statement is not performed Control proceeds with the next statement after the for statement Control variable Often printed or used inside for body, but not necessary
Good Programming Practice Place only expressions involving the control variables in the initialization and increment sections of a for statement. Manipulations of other variables should appear either before the loop (if they execute only once, like initialization statements) or in the loop body (if they execute once per repetition, like incrementing or decrementing statements). Although the value of the control variable can be changed in the body of a for loop, this can lead to subtle errors. It is best not to change it. Although statements preceding a for and statements in the body of a for can often be merged into the for header, avoid doing so because it makes the program more difficult to read. Limit the size of control-statement headers to a single line if possible.
In-Class Programming Exercise Create a function that will find the factorial of a number. Be sure to include a default case for the number zero. Example output: Enter your number: 3 The factorial of 3 is 6!
- Slides: 14