for statement c code question
yes I'm using the book, teach yourself c in 21 days which I got off the net, and in showing to use nested for statements it uses this as an example:
1: /* Demonstrates nesting two for statements */ 2: 3: #include <stdio.h> 4: 5: void draw_box( int, int); 6: 7: main() 8: { 9: draw_box( 8, 35 ); 10: 11: return 0; 12: } 13: 14: void draw_box( int row, int column ) 15: { 16: int col; 17: for ( ; row > 0; row--) 18: { 19: for (col = column; col > 0; col--) 20: printf("X"); 21: 22: printf("\n"); 23: } 24: } I get how it works, but in the book it says that it doesn't work right if you don't substitute column for col in the nested for statements. I tried it in my compiler, dev-c++, and they were right. Would anyone be able to figure out why this might be? I'm very much stumped on this..... |
well if u donot substitute the value column will become 1 after first iteration of outer loop
in the second iteration (and after that)inner for loop wont be executed at all so its essential to substitute the value of column for col |
Every time the inner loop runs, it has to start again with the value pased to the "column" parameter. If you don't substitue it, you'll change "column" itself. Thus losing the original value.
|
yah, i see it now, thanks a lot for your answers, this stupidly stumped me for a good 10 min. ^^;
|
All times are GMT -5. The time now is 03:52 AM. |