Page 1 of 1

Odd behaviour of the compiler when using loops

Posted: Tue Nov 13, 2012 7:44 pm
by Foxi4
I was wondering whether to put this in the libnds section or here, but figured that compilation rules fall under devkitARM.

Here's the issue - the standard syntax in a for loop in C is

for(StartingExpression; TestingExpression;Count){...}

...so for example

for(i=0;i<5;i++){...}

and this of course works. However, when I use

for(i=0;i<6;i+2){...}

compilation fails. Instead, I have to use a whole assignment to create a full statement for Count, such as

for(i=0;i<6;i=i+2){...}
or
for(i=0;i<6;i+=2){...}

I've read examples and texts on the subject listing the second statement as entirely valid - why the error? Has the standard changed?

I'm working on Windows 7 x64 with an unaltered, latest version of the kit and I'm using the make command to compile, thanks in advance for any replies and insight on the cause of this behaviour. Does the last argument have to be a whole expression?

Re: Odd behaviour of the compiler when using loops

Posted: Tue Nov 13, 2012 8:28 pm
by elhobbs
you are not quite right...
for(i=0;i<6;i+2){...}
is not valid

more like this

Code: Select all

for(initialize;condition;evalute after each loop before condition) {
    //do stuff
}
it works like this

Code: Select all

initialize;
while(condition) {
    //do stuff
    evalute after each loop before condition
}

Re: Odd behaviour of the compiler when using loops

Posted: Tue Nov 13, 2012 8:34 pm
by Foxi4
Well, that explains everything! It must have been a mistake in the example then - thank you for the fast response! I take it that I can still use commas to separate expressions that I want to use together? Like:

for(i=0;i<5;i++, foo--){...}

Sorry for not checking myself - I'm on mobile so I can't compile. ;P

Re: Odd behaviour of the compiler when using loops

Posted: Tue Nov 13, 2012 10:19 pm
by elhobbs
Foxi4 wrote:Well, that explains everything! It must have been a mistake in the example then - thank you for the fast response! I take it that I can still use commas to separate expressions that I want to use together? Like:

for(i=0;i<5;i++, foo--){...}

Sorry for not checking myself - I'm on mobile so I can't compile. ;P
yes, that is correct.

Re: Odd behaviour of the compiler when using loops

Posted: Wed Nov 14, 2012 11:45 pm
by Foxi4
Perfect! All questions answered then, the thread can be safety locked. :)