Odd behaviour of the compiler when using loops

support for the ARM toolchain
Post Reply
Foxi4
Posts: 14
Joined: Wed Feb 22, 2012 10:49 pm

Odd behaviour of the compiler when using loops

Post by Foxi4 » Tue Nov 13, 2012 7:44 pm

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?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Odd behaviour of the compiler when using loops

Post by elhobbs » Tue Nov 13, 2012 8:28 pm

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
}

Foxi4
Posts: 14
Joined: Wed Feb 22, 2012 10:49 pm

Re: Odd behaviour of the compiler when using loops

Post by Foxi4 » Tue Nov 13, 2012 8:34 pm

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

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Odd behaviour of the compiler when using loops

Post by elhobbs » Tue Nov 13, 2012 10:19 pm

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.

Foxi4
Posts: 14
Joined: Wed Feb 22, 2012 10:49 pm

Re: Odd behaviour of the compiler when using loops

Post by Foxi4 » Wed Nov 14, 2012 11:45 pm

Perfect! All questions answered then, the thread can be safety locked. :)

Post Reply

Who is online

Users browsing this forum: No registered users and 28 guests