1 registered members (AndrewAMD),
1,577
guests, and 7
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: for loop with blank comparison statement
[Re: WretchedSid]
#358534
02/11/11 20:50
02/11/11 20:50
|
Joined: Feb 2011
Posts: 135
Myrkling
OP
Member
|
OP
Member
Joined: Feb 2011
Posts: 135
|
Yes, normally empty statements in for loops shouldn't do any harm. However, lite-C doesn't seem to like empty comparison statements. Please have a look at the following two examples. They seem to cause problems reliably. 1) This script freezes the engine. i is never incremented even though it should. Replacing the empty comparison statement with '1' solves the problem.
#include <acknex.h>
void main()
{
int i;
for (i = 0; ; i++)
{
if (i > 10)
break;
}
}
2) Open "infinite_terrain.c" from the samples folder and paste the following code right at the end of the script (after the main function):
void let_it_crash()
{
int i;
for (i = 0; ; i++)
{
if (i > 10)
break;
}
}
Then run "infinite_terrain.c". The engine should crash immediately ("Script crash in SYS"). Replacing the empty comparison statement with '1' solves the problem.
|
|
|
Re: for loop with blank comparison statement
[Re: Myrkling]
#358535
02/11/11 20:55
02/11/11 20:55
|
Joined: Oct 2007
Posts: 5,210 İstanbul, Turkey
Quad
Senior Expert
|
Senior Expert
Joined: Oct 2007
Posts: 5,210
İstanbul, Turkey
|
cause of the crash is not the empty comparison, nor replacing it with 1 solves the problem.
problem is that the if(i > 10) break; seems to be never executed, wihich results in an infinite loop.
3333333333
|
|
|
Re: for loop with blank comparison statement
[Re: Quad]
#358539
02/11/11 21:13
02/11/11 21:13
|
Joined: Feb 2011
Posts: 135
Myrkling
OP
Member
|
OP
Member
Joined: Feb 2011
Posts: 135
|
I can't confirm that. The following change to the loop of the first example always prints '0' for me. That implies that i doesn't increment.
for (i = 0; ; i++)
{
printf("%i", i);
if (i > 10)
break;
}
|
|
|
Re: for loop with blank comparison statement
[Re: Myrkling]
#358543
02/11/11 21:28
02/11/11 21:28
|
Joined: Oct 2004
Posts: 900 Lgh
rojart
User
|
User
Joined: Oct 2004
Posts: 900
Lgh
|
Try this way:
function main(){
int i;
for (i = 0;i < 100; i++){
printf("%i",i);
if (i >= 10)
break;
}
}
or
function main(){
int i=0;
for (;;){
i++;
printf("%i",i);
if (i > 10)
break;
}
}
Without comparison, works only like second example.
Last edited by rojart; 02/11/11 21:47.
|
|
|
Re: for loop with blank comparison statement
[Re: rojart]
#358547
02/11/11 21:53
02/11/11 21:53
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
rojart, both versions are a bit too "much", as expression you only need something that evaluates the whole time to true, eg. anything non 0. Like:
function main(){
int i;
for (i = 0; 1; i++){
printf("%i",i);
if (i >= 10)
break;
}
}
(This is also what the C standard describes what a compiler should insert in the case a user leaves the field blank) Edit: The loop will increase the iterator at the end of the loop, before evaluating the expression again. So your second code sample is completely wrong.
Last edited by JustSid; 02/11/11 22:11.
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: for loop with blank comparison statement
[Re: rojart]
#358553
02/11/11 23:01
02/11/11 23:01
|
Joined: Apr 2007
Posts: 3,751 Canada
WretchedSid
Expert
|
Expert
Joined: Apr 2007
Posts: 3,751
Canada
|
Well, you might have no expression added, but the compiler will do it for you anyway. Source: 6.8.5.3 The for statement 1 The statement for ( clause-1 ; expression-2 ; expression-3 ) statement behaves as follows: The expression expression-2 is the controlling expression that is evaluated before each execution of the loop body. The expression expression-3 is evaluated as a void expression after each execution of the loop body. If clause-1 is a declaration, the scope of any identifiers it declares is the remainder of the declaration and the entire loop, including the other two expressions; it is reached in the order of execution before the first evaluation of the controlling expression. If clause-1 is an expression, it is evaluated as a void expression before the first evaluation of the controlling expression. 137) 2 Both clause-1 and expression-3 can be omitted. An omitted expression-2 is replaced by a nonzero constant
http://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf (page 135)
Shitlord by trade and passion. Graphics programmer at Laminar Research. I write blog posts at feresignum.com
|
|
|
Re: for loop with blank comparison statement
[Re: WretchedSid]
#358557
02/11/11 23:44
02/11/11 23:44
|
Joined: Oct 2004
Posts: 900 Lgh
rojart
User
|
User
Joined: Oct 2004
Posts: 900
Lgh
|
An omitted expression-2 is replaced by a nonzero constant Well, then it should to work like in the first example from Myrkling, a bug?
|
|
|
|