LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 12-30-2003, 10: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 */
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.....
 
Old 12-30-2003, 10:27 PM   #2
sashhoney
Member
 
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, 07:55 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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, 11:17 AM   #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. ^^;
 
  


Reply



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 01:37 AM
DEBUG statement question djgerbavore Programming 3 01-26-2005 09:37 AM
Bad switch statement (problem in the code block?) nifflerX Linux - General 2 07-14-2004 03:45 PM
bash if statement question xscousr Programming 3 09-02-2003 11:58 AM
question about C 'for' statement ... purpleburple Programming 8 08-25-2002 07:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:17 AM.

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