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 support@ccsinfo.com

multiple expression in a single For Statement

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








multiple expression in a single For Statement
PostPosted: Wed May 26, 2004 7:31 pm     Reply with quote

hi,

anyone in the community who can provide a sample snippet of a multiple expression for statement?

I'll appreciate any help.

Thank u.
future



Joined: 14 May 2004
Posts: 330

View user's profile Send private message

PostPosted: Wed May 26, 2004 8:34 pm     Reply with quote

int8 var;
int8 var1;
int8 var2;

for(var=0;var<255;var++) {
var1++;
var2++;
var1+=var2;
};

Is it all?
Radix
Guest







PostPosted: Thu May 27, 2004 5:48 am     Reply with quote

init-expression and loop-expression can contain multiple statements separated by the comma operator. For example:

Code:

// for_statment3.cpp
#include <stdio.h>
int main()
{
   int i, j;
   for ( i = 5, j = 10 ; i + j < 20; i++, j++ )
      printf( "\n i + j = %d", (i + j) );
}


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_c.2b2b_.for_statement.asp

Regards
Ttelmah
Guest







PostPosted: Thu May 27, 2004 6:57 am     Reply with quote

Radix wrote:
init-expression and loop-expression can contain multiple statements separated by the comma operator. For example:

Code:

// for_statment3.cpp
#include <stdio.h>
int main()
{
   int i, j;
   for ( i = 5, j = 10 ; i + j < 20; i++, j++ )
      printf( "\n i + j = %d", (i + j) );
}


http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_the_c.2b2b_.for_statement.asp

Regards

This is true for just about any 'statement'. Also, if you put a bracket round the statement group, it is the last element that is returned. So you can say (for instance)
for (i=(buff[0]=fred, buff[2]),....)

and here buff[0] will be set to the value of 'fred', while i will be set to buff[2]. This is particularly useful for generating multi-line '#define' statements, where (for instance), you can define a function to get a character from a circular buffer, like:
int8 btemp;
#define frombuff(buff,in,out,size) (btemp=out,\
out=(++out) % size, \
buff[btemp])

C is very powerful in this regard, but it can lead to very difficult to read code, and if the compiler optimisations are working properly, does not help to reduce the size of code...
Use with care.

Best Wishes
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Thu May 27, 2004 4:59 pm     Reply with quote

True. Once I saw some dude's code which solved the 8 queens puzzle in only one 'for' statement (and it wasn't that long either). Pure genius, but almost impossible to understand.

I wouldn't recommend doing any complicated multi-statements in CCS, as it is asking for trouble. The main rule in using CCS compiler is: stay simple.
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