Download presentation
Presentation is loading. Please wait.
Published byEugenia Doyle Modified over 8 years ago
1
CS 161 Introduction to Programming and Problem Solving Chapter 17 Nested Loops Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from ECE 103 material by prof. Phillip Wong @ PSU
2
Syllabus Nested while and for Loops Tracing Through a Loop Examples
3
2 Nested Loops A loop (e.g., while, do-while, for ) can be placed within the body of another loop Nested loops can be multiple levels deep (e.g., loop within a loop within a loop, etc.) For a nested loop, a break statement only exits out of the loop that contains the break
4
3 Nested while Loop: Typical 2-level nested while loop: while ( expr1 ) { : while ( expr2 ) { : Update expr2 ; } : update expr1; } ← outer loop ← inner loop
5
4 Example: 2-level nested while loop #include int main (void) { // main int row, col; row = 0; while( row < 3 ) { // note: magic number! col = 0; while( col < 3 ) { // ditto: Magic number! printf( "(%2d %2d) ", row, col ); col++; } //end while printf( "\n” ); row++; } //end while return 0; } //end main ( 0 0) ( 0 1) ( 0 2) ( 1 0) ( 1 1) ( 1 2) ( 2 0) ( 2 1) ( 2 2)
6
5 Nested for Loop: Typical 2-level nested for loop: for ( expr1a; expr2a; expr3a ) { : for ( expr1b; expr2b; expr3b ) { : } : } ← outer loop a ← inner loop b
7
6 Example: 3-level nested for loop for( level = 1; level <= 3; level++ ) { printf( "Level = %d\n", level ); for( R = 0; R < 3; R++ ) { for( C = 0; C < 3; C++ ) printf( "%3d ", R*C*level ); printf( "\n” ); } //end for
8
7 Example: 3-level nested for loop for( level = 1; level <= 3; level++ ) { printf("Level = %d\n", level ); for( R = 0; R < 3; R++ ) { for( C = 0; C < 3; C++ ) { printf( "%3d ", R*C*level ); } //end for printf( "\n” ); } //end for Level = 1 0 0 0 0 1 2 0 2 4 Level = 2 0 0 0 0 2 4 0 4 8 Level = 3 0 0 0 0 3 6 0 6 12
9
8 Example: for loop inside while loop /* computes factorial( num ) */ #include int main( void ) { // main int num; /* Input value */ long prod = 0; /* Accumulator for partial product */ int i, Done = 0; /* Index and flag */ while( !Done ) { printf( "Enter number: ” ); scanf( "%d", &num ); if( num < 0 ) { // Exit if user enters negative value Done = 1; }else{ prod = 1; for( i = 1; i <= num; i++ ) { prod *= i; } //end for printf( "factorial of %d = %d\n\n", num, prod ); } //end if } //end main
10
9 Tracing Through a Loop You can get a good understanding of loops by writing out a table of values The table shows the current state at the start of each iteration The columns of the table list the variables or calculations of interest at each iteration
11
10 Example: for( m = 1; m <= 3; m++ ) { s = m*m; printf("%d %d\n", m, s); } //end for Iteration #ms = m*mprintf(…) 1111 2242 4 3393 9
12
11 Suppose you have a 2-level nested loop. The outer loop performs m iterations, and the inner loop does n iterations: This means: For each iteration of the outer loop, the inner loop is executed n times Total number of iterations is m × n
13
12 Example: for( m = 1; m <= 3; m++ ){ s = 0; for( n = 1; n <= 3; n++ ) { // number iterations SAME s += m*n; printf( "%d %d %d\n", m, n, s ); } //end for Iteration #mns += m*nprintf(…) 11111 1 1 21231 2 3 31361 3 6 42122 1 2 52262 2 6 62312 2 3 12 73133 1 3 83293 2 9 93318 3 3 18
14
13 Example: for( m = 1; m <= 3; m++ ) { s = 0; for( n = m; n <= 3; n++ ) { s += m*n; printf("%d %d %d\n", m, n, s);} //end for Iteration #mns += m*nprintf(…) 11111 1 1 21231 2 3 31361 3 6 42242 2 4 52310 2 3 10 63393 3 9 Here the number of iterations in the inner loop is controlled by the outer loop index
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.