CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

silly question FOR LOOP

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
bschriek



Joined: 18 Dec 2007
Posts: 80

View user's profile Send private message Send e-mail

silly question FOR LOOP
PostPosted: Mon Dec 12, 2022 2:42 am     Reply with quote

I have some silly questions and hopefully an experts can give me a short answer.


1)
For (i=10; i>=0; --i)
{
printf("%u",i); // Result is 10,9,8,7,6,5,4,3,2,1 So far so good.
}
- Why use "i>=0" for expr2 of the FOR LOOP and not "i>0"?

- Sometimes I see "--i" for expr3 of the FOR LOOP and sometimes i--, what's the difference?



2)
For (i=10; i>=0; --i)
{
printf("%u",i); // Now I want the following results 10,9,8,7,6,5,4,3,2,1,0
// (ofcourse i>=0 doesn't work here, but what will work?)
}

Is there a simple solution to use the FOR LOOP without additional code?


As I told you before, stupid questions but I'm just curious.
Thanks you in advance,
Ttelmah



Joined: 11 Mar 2010
Posts: 19540

View user's profile Send private message

PostPosted: Mon Dec 12, 2022 6:39 am     Reply with quote

The point about --i versus i--, is that in the first case the decrement is done
_before_ the value is passed. In the second afterwards. This matters when
a value decremented like this is being used in mathematics, but not here.

So:
With i=10;

j=i--;

j receives 10, and i is 9 after the operation.

But:

j=--i;

j receives 9, and again i is 9 afterwards.

In the loop, the test is done on the result of the operation, so it makes
no difference.

Now there is a huge caveat in what you post here. It will not work if 'i' is
not a signed variable. An unsigned number can never go less than 0.
You are printing the variable as an unsigned, while the actual value needs
to be a signed to work.

So:
Code:

      signed int i; //Critical

      for (i=10; i>=0; --i)
      {
         printf("%d,",i); // Will print 10,9,8,7,6,5,4,3,2,1,0
      }

I've added a ',' so it prints exactly what you are asking for.


Last edited by Ttelmah on Mon Dec 12, 2022 6:42 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Dec 12, 2022 6:42 am     Reply with quote

ok, I was never taught C but...
..my understanding....

1) When the PIC decrements the variable 'i' from '1', it could become 00 or FF ,depending on when the 'if' statement is executed ( kind of a 'race' condition ? ) and HOW it's done. Actually have to see the assembler. for '0' you could check the ZERO flag after the comparison is done.

2) --i means to subtract from i BEFORE the interior for-loop code is executed, and i-- means subtract AFTER...that stuff is executed. One way you get 1 extra for..loop....well ,pretty sure..

What I am sure of is someone will reply and tell us !
bschriek



Joined: 18 Dec 2007
Posts: 80

View user's profile Send private message Send e-mail

PostPosted: Mon Dec 12, 2022 6:58 am     Reply with quote

Dear all,

Hihi, oh it's that simple.
I never thought about using a signed int but you are right. Thanks.

And about --i or i--. I understand there is no difference in case of a FOR LOOP.

Thank you for your clear answer.

Best regards,
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group