943,901 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1053
  • C RSS
Oct 31st, 2006
0

post decriment in while

Expand Post »
I hav one doubt regarding post decriment,the code in c is as follows.............

void main()
{
clrscr();
while(i--!=6)
i=i+2;
printf("%d\n",i);
getch();
}

I know the output is 5, but i read in a book dat the post incriment or decrement operators are evaluated after reaching semicolon(.....but in while condition,the ondition fails so it wont execute the next line so, directly reaches printf.Till now there is no semicolon still the i value is decresed......wts da reason...can u sugest me..?

Thanks & Regards.
Similar Threads
Reputation Points: 12
Solved Threads: 1
Light Poster
pointers is offline Offline
34 posts
since Oct 2006
Oct 31st, 2006
0

Re: post decriment in while

If you want help you must to fix your code. i is undefined and uninitialised. There are more problems with your code but first fix and beautify it.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Oct 31st, 2006
1

Re: post decriment in while

Wow.
  1. void main() - don't use void main. main() returns an int.
  2. {
  3. clrscr();
  4. while(i--!=6) - where is i set to some intitial value?
  5. i=i+2; - this is the end of the while loop
  6. I think you are missing {} to make a while block?
  7. printf("%d\n",i);
  8. getch();
  9. }
  10.  
You do realize that your while loop subtracts then adds to the same variable, essentially in the same statement. Eeeek...

Back up.

What are you trying to do?
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: pascal triangle
Next Thread in C Forum Timeline: Problem with using % !





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC