Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-30-2003, 11:01 PM   #1
dragoon linux
LQ Newbie
Registered: Nov 2003
Posts: 24

Rep: Reputation: 15
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 */
3: #include <stdio.h>
5: void draw_box( int, int);
7: main()
8: {
9: draw_box( 8, 35 );
11: return 0;
12: }
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");
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.....
Old 12-30-2003, 11:27 PM   #2
Registered: Jul 2003
Distribution: Red Hat, Fedora, Debian
Posts: 85

Rep: Reputation: 15
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
Old 12-31-2003, 08:55 AM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 110Reputation: 110
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.
Old 12-31-2003, 12:17 PM   #4
dragoon linux
LQ Newbie
Registered: Nov 2003
Posts: 24

Original Poster
Rep: Reputation: 15
yah, i see it now, thanks a lot for your answers, this stupidly stumped me for a good 10 min. ^^;


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 02:37 AM
DEBUG statement question djgerbavore Programming 3 01-26-2005 10:37 AM
Bad switch statement (problem in the code block?) nifflerX Linux - General 2 07-14-2004 04:45 PM
bash if statement question xscousr Programming 3 09-02-2003 12:58 PM
question about C 'for' statement ... purpleburple Programming 8 08-25-2002 08:12 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:32 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration