LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 10-19-2003, 05:43 AM   #1
wuck
Member
 
Registered: Sep 2003
Location: barneveld.nl
Distribution: Slackware
Posts: 170

Rep: Reputation: 30
[C] for looping problem


I'm almost sure to be right about the following:

If you've got two variables, x and argc, (used for command-line input) and you want to make a loop where x will constantly be raised by one, started with value 1, until the value of argc is reached.
To me, the most neat approach to do that is by using for:

Code:
for ( x = 1 ; x == argc ; x++ )
{

 ( ... )

}
Now I just can't believe my eyes, because on my box, this does not work. All code (the ( ... ) ) simply won't be executed. The only way to get this working, is the following:

Code:
int main(int argc, char **argv)
{
	int x;

	if (argc == 1)
		PrintUsage ();
	else
		for ( x = 1 ; ; x++ )
		{
			if (x == argc)
				break;

			printf ("%i %s\n", x, argv[x]);

		}



	Head ();
	Body ();
	Tail ();
}
Have I overlooked something?The above example _does_ work, but the first should, too! Right?
 
Old 10-19-2003, 06:07 AM   #2
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 33
Re: [C] for looping problem

This is the miskate you are making
Code:
for ( x = 1 ; x <= argc  ; x++ )
//You used x==argc
{
 ( ... )
}
The for loop should continue until the value of argc has been reached n so the condition should be x<=argc and not x==argc
 
Old 10-19-2003, 10:05 AM   #3
wuck
Member
 
Registered: Sep 2003
Location: barneveld.nl
Distribution: Slackware
Posts: 170

Original Poster
Rep: Reputation: 30
So:

Code:
for ( x = 1 ; !(x == argc) ; x++)
Should work, too?
 
Old 10-19-2003, 10:10 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Sure, as well as:
Code:
for (x = 1; x != argc; x++)
though, aesthetically, SaTaN's code is the best of the three.
 
Old 10-19-2003, 10:38 AM   #5
wuck
Member
 
Registered: Sep 2003
Location: barneveld.nl
Distribution: Slackware
Posts: 170

Original Poster
Rep: Reputation: 30
Thank you both.
 
Old 10-19-2003, 11:44 AM   #6
Kurt M. Weber
Member
 
Registered: Oct 2003
Distribution: Slackware
Posts: 335

Rep: Reputation: 36
Well, they're not quite the same.

Satan's will execute the loop so long as x is less than or equal to argc, while the other two examples will only execute when x is not equal to argc (meaning that when x is equal to argc, the loop will terminate before it goes through).
 
Old 10-19-2003, 04:29 PM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
True, but that only makes satan's code the better o the three. Because x is incremented by 1 the result in this case is the same.
 
  


Reply


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
login looping plaircpa Slackware 3 10-31-2005 08:10 PM
SDL audio looping mala fide Programming 0 05-11-2005 09:47 AM
print job looping problem driving me insane sl1dey Solaris / OpenSolaris 2 09-06-2004 09:38 PM
looping in mplayer zexter Linux - Software 1 12-22-2003 04:39 PM
while loop not looping as intended Tarts Programming 7 11-28-2003 02:50 AM

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

All times are GMT -5. The time now is 10:42 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