post decriment in while

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 34
Reputation: pointers is an unknown quantity at this point 
Solved Threads: 1
pointers pointers is offline Offline
Light Poster

post decriment in while

 
0
  #1
Oct 31st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: post decriment in while

 
0
  #2
Oct 31st, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: post decriment in while

 
1
  #3
Oct 31st, 2006
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?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC