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

for statement

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



Joined: 30 Jan 2010
Posts: 8

View user's profile Send private message

for statement
PostPosted: Wed Feb 10, 2016 1:53 am     Reply with quote

Hello,

is there a difference between ++idx or idx++ in a for statement? There is not in c#.

e.g.

Code:
for(idx = 0; idx < CmdIndex; idx++){
      Buffer[idx] = Buffer[idx + 1];
}

Code:

for(idx = 0; idx < CmdIndex; ++idx){
      Buffer[idx] = Buffer[idx + 1];
}

Thanx
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Wed Feb 10, 2016 3:43 am     Reply with quote

This is the same in C, C++, C#, and Java (and probably most other languages).

++n, increments 'n' _before_ using it in the operation.
n++, increments 'n' _after_ using it in the operation.

Now, as used here in the for loop, it'll make no apparent difference, since you make no other 'use' of n inside the operation where the increment occurs. However, if you make use it:
Code:

#include <18F4520.h>
#device ADC=10
#use delay(crystal=20000000)
#use rs232(UART1,ERRORS,BAUD=9600)

void display(int8 n)
{
   //demo, just output n^2 on a line
   printf("%d\n",n*n);
}

void main()
{
   int8 n;
   for (n=0;n<8;display(++n))
      ;//whatever else you want here;
   
   while(TRUE)
      ;
}


Now here, instead of just incrementing it, I print out 'n^2' in a little statement.

If you use ++n, you see:
1
4
9
16
25
36
49
64

However, use n++, and you get:
0
1
4
9
16
25
36
49

Note the difference.

So because you are doing nothing 'else' with 'n' at this point, it makes no difference if you increment first or second. However if your code did anything else with 'n', then it matters.
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Wed Feb 10, 2016 4:08 am     Reply with quote

It is conventional to post increment (i++) or decrement in a for loop, but not required. Also, the "increment" is in fact a statement that's executed at the end of each loop, and as its a statement, it can be very complicated. Effectively the work of a program can be done in a for loop statement. It can also be empty, doing nothing. Though its conventional, and therefore advisable to keep it short and simple. Personally, if I'm doing something other than a simple increment/decrement, I add comments to explain why I coded it that way and how it works.

Code:

while(TRUE)
{
... Do main loop of program
}

// can be, and often was in C's early years:

for(;;)
{
... Do main loop of program
}

// and could be, if one wanted to be obtuse:

for(;; {
... Do main loop of program
});  // Note there's no body of the for loop here - its all done in the "increment".
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