LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 06-09-2007, 01:47 AM   #1
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Rep: Reputation: 33
Weird 'for' loops in C


Here are some for loops which compile and run properly but I have no idea how they work.

Code:
#include <stdio.h>

int main()
{
int i=0;

for(;i;)
 printf("\n Sentence");

return 0;
}
output: Nothing.

No error even though the second statement in the for loop is not a conditional statement?

Here's another:

Code:
#include <stdio.h>

int main()
{
int i;
for(i=1; i<=5; printf("\n %d",i));
i++;

return 0;
}
Output: Infinite 1's

How do these programs work? I only know how common for loops work.
 
Old 06-09-2007, 01:54 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
The second statement in the first for loop is a conditional statement - it's just false since i = 0. If i was initialised with any other number, it would be true.
 
Old 06-09-2007, 02:00 AM   #3
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
You are correct, I put i = 1 and got an infinite loop. May I know from where did you learn this? I never learned that you could substitute true with any number but zero and false with 0 in a C program.

What about the second loop??

Last edited by gregorian; 06-09-2007 at 02:04 AM.
 
Old 06-09-2007, 02:31 AM   #4
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 61
This is just part of C. As you learn C, you will pick these things up. Of course, they would not be considered "good programming practice".

--Ian
 
Old 06-09-2007, 03:01 AM   #5
Valkyrie_of_valhalla
Member
 
Registered: Jan 2006
Location: Romania
Distribution: Suse 12.0, Slackware 12.1, Debian, Ubuntu, Gentoo
Posts: 301

Rep: Reputation: 30
about the second loop.
Try it like this:
Code:
#include <stdio.h>

int main()
{
int i;
for(i=1; i<=5; i++)
printf("\n %d",i);

return 0;
}
EDIT: don't put ";" after a for statement, as everything that follows after that for doesn't get included in the for statement. It also works how you mentioned if you remove that ";", but it would print out 2 3 4 5 6. So I would reccomend initialising i with 0.

Last edited by Valkyrie_of_valhalla; 06-09-2007 at 03:06 AM.
 
Old 06-09-2007, 03:28 AM   #6
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Thank you Valkyrie.

I have now understood how the programs work:

The for loop works by initialising the counter, checking for the validity of the second statement, executing the body of the loop, and then executing whatever is there after the second semi colon. Since the only body of the for loop in the original program (second one) was the semi colon, the value of the counter never got incremented and thus it broke into an infinite loop, continuously executing the statement after the second semi colon - the printf statement which gave the unincremented value of i i.e 1 continuously.

Thank you all for your help.

One more question: Why can't I declare the for loop variable in the for loop itself i.e.

Code:
for(int i=1; i<5; i++)
instead of

Code:
int i;
for(i=1; i<5; i++)
I get this error with gcc:

test.c: In function ‘main’:
test.c:6: error: ‘for’ loop initial declaration used outside C99 mode

Last edited by gregorian; 06-09-2007 at 03:45 AM.
 
Old 06-09-2007, 04:13 AM   #7
IBall
Senior Member
 
Registered: Nov 2003
Location: Perth, Western Australia
Distribution: Ubuntu, Debian, Various using VMWare
Posts: 2,088

Rep: Reputation: 61
Exactly as the error says - this is not complient with the ANSI C99 standard. There are gcc flags that will turn this off, but it is not really a problem.

Older versions of C, as well as some other languages permit this shortcut.

It is generally considered better programming practice to declare the variables separately from the initializations.

--Ian
 
Old 06-09-2007, 05:14 AM   #8
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Ok, thank you!
 
Old 06-09-2007, 05:48 AM   #9
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
I was supposed to write a program that adds the first 7 terms of

1/1! + 2/2! + ...

I wrote this:

Code:
#include <stdio.h>

main()
{

int i,m,fact;
float sum,div;

sum=i=m=0;

/* First seven times */

for(i=1; i<=7 ; i++)
 {
  fact =1; 

/* Factorial function */

  for(m=1; m<=i; m++) 
    fact*=m;
    
   div = i/fact; 
      
  sum += div;

printf("%d / %d = %f sum = %f\n",i,fact,div,sum);

 }



}
This is the output:

Quote:
1 / 1 = 1.000000 sum = 1.000000
2 / 2 = 1.000000 sum = 2.000000
3 / 6 = 0.000000 sum = 2.000000
4 / 24 = 0.000000 sum = 2.000000
5 / 120 = 0.000000 sum = 2.000000
6 / 720 = 0.000000 sum = 2.000000
7 / 5040 = 0.000000 sum = 2.000000
3/6 = 0.000000
 
Old 06-09-2007, 06:07 AM   #10
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
You're doing integer division. You need to cast either the numerator or the denominator to a float.
 
Old 06-09-2007, 06:22 AM   #11
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Thanks for that!
 
Old 06-09-2007, 06:42 AM   #12
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Quote:
Originally Posted by gregorian
I was supposed to write a program that adds the first 7 terms of

1/1! + 2/2! + ...
You can reduce the amount of round off error if you decrement from 7 to 1 instead.
 
Old 06-09-2007, 06:43 AM   #13
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Out of interest, does that series have a name?
 
Old 06-09-2007, 08:51 AM   #14
gregorian
Member
 
Registered: Apr 2006
Location: India
Posts: 473

Original Poster
Rep: Reputation: 33
Yes, it's called 'e' as in the 'e' of natural logarithms if you sum the terms till infinity.

Could you explain jschiwal? How's the error minimised if I add the series backwards?

Output:

Quote:
1 / 1 = 1.000000 sum = 1.000000
2 / 2 = 1.000000 sum = 2.000000
3 / 6 = 0.500000 sum = 2.500000
4 / 24 = 0.166667 sum = 2.666667
5 / 120 = 0.041667 sum = 2.708333
6 / 720 = 0.008333 sum = 2.716667
7 / 5040 = 0.001389 sum = 2.718056
The final result here is correct uptil 3 decimal places.

Last edited by gregorian; 06-09-2007 at 08:56 AM.
 
Old 06-11-2007, 02:49 AM   #15
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
Quote:
Originally Posted by gregorian
Yes, it's called 'e' as in the 'e' of natural logarithms if you sum the terms till infinity.

Could you explain jschiwal? How's the error minimised if I add the series backwards?

Output:



The final result here is correct uptil 3 decimal places.
Each term is smaller than the last one. Your series is so short that you may not notice much of a problem, but for some series, the lost of significant digits from adding to 1.0 instead of a bunch of terms like 1.2234567e-13 being totaled can effect the final result.
For the smallest terms in a longer series, you will be adding 0 to the total.

By the way, you might want to look at the manpage of "bc" for the library function they use for e(x).

Try this on for size!
echo 'scale=1024; print e(1) ' | bc -l

Last edited by jschiwal; 06-12-2007 at 04:10 AM.
 
  


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
Nesting while loops rujin69 Programming 2 03-20-2007 05:17 AM
while loops + while : blizunt7 Linux - General 3 12-04-2004 05:27 PM
variable with while loops michael_util Programming 4 08-16-2004 12:07 PM
loops JMK Linux - Newbie 11 04-09-2004 05:30 PM
Functions And Loops petercool Programming 14 08-08-2003 10:35 AM


All times are GMT -5. The time now is 02:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration