Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 19 Nested Loops

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 19 Nested Loops"— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 19 Nested Loops
Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

2 Syllabus Nested while and for Loops Tracing Through a Loop Examples

3 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 Nested while Loop: Typical 2-level nested while loop: while (expr1) {
Update expr2; } update expr1; ← outer loop ← inner loop

5 Example: 2-level nested while loop
#include <stdio.h> 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++; return 0; } //end main ( 0 0) ( 0 1) ( 0 2) ( 1 0) ( 1 1) ( 1 2) ( 2 0) ( 2 1) ( 2 2) 4

6 Nested for Loop: Typical 2-level nested for loop:
for ( expr1a; expr2a; expr3a ) { : for ( expr1b; expr2b; expr3b ) } ← outer loop a ← inner loop b

7 Example: 3-level nested for loop
// level and R are defined 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 6

8 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” ); Level = 1 Level = 2 Level = 3 7

9 Example: for loop inside while loop
/* computes factorial( num ) */ #include <stdio.h> 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 8

10 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 for( m = 1; m <= 3; m++ ) { Example: s = m*m;
printf("%d %d\n", m, s); } //end for Iteration # m s = m*m printf(…) 1 1 1 2 4 2 4 3 9 3 9 10

12 Suppose you have a 2-level nested loop
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 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 } //end for Iteration # m n s += m*n printf(…) 1 2 3 6 4 5 12 7 8 9 18 12

14 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 } //end for Here the number of iterations in the inner loop is controlled by the outer loop index Iteration # m n s += m*n printf(…) 1 2 3 6 4 5 10 9 13


Download ppt "ECE 103 Engineering Programming Chapter 19 Nested Loops"

Similar presentations


Ads by Google