Gamestudio Links
Zorro Links
Newest Posts
Zorro Beta 2.61: PyTorch
by jcl. 06/10/24 14:42
New FXCM FIX Plugin
by flink. 06/04/24 07:30
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 1,577 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
AemStones, LucasJoshua, Baklazhan, Hanky27, firatv
19058 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 2 of 2 1 2
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 Offline OP
Member
Myrkling  Offline 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.
Code:
#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):
Code:
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 Offline
Senior Expert
Quad  Offline
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 Offline OP
Member
Myrkling  Offline 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.
Code:
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 Offline
User
rojart  Offline
User

Joined: Oct 2004
Posts: 900
Lgh
Try this way:
Code:
function main(){
	int i;
	
	for (i = 0;i < 100; i++){
		printf("%i",i);
		if (i >= 10)
		break;
	}
}

or
Code:
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.

Regards, Robert

Quote
Everything should be made as simple as possible, but not one bit simpler.
by Albert Einstein

PhysX Preview of Cloth, Fluid and Soft Body

A8.47.1P
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 Offline
Expert
WretchedSid  Offline
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:
Code:
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: WretchedSid] #358552
02/11/11 22:52
02/11/11 22:52
Joined: Oct 2004
Posts: 900
Lgh
rojart Offline
User
rojart  Offline
User

Joined: Oct 2004
Posts: 900
Lgh
ok, changed and it works like your code, but without comparison. wink

Code:
function main(){
	int i;
	
	for (i=0;;){
		
		printf("%i",i);
		if (i >= 10)
		break;
		i++;
	}
}




Regards, Robert

Quote
Everything should be made as simple as possible, but not one bit simpler.
by Albert Einstein

PhysX Preview of Cloth, Fluid and Soft Body

A8.47.1P
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 Offline
Expert
WretchedSid  Offline
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:
Quote:

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 identi&#64257;ers 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 &#64257;rst evaluation of the controlling expression. If clause-1 is an expression, it is
evaluated as a void expression before the &#64257;rst 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 Offline
User
rojart  Offline
User

Joined: Oct 2004
Posts: 900
Lgh
Originally Posted By: JustSid
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?


Regards, Robert

Quote
Everything should be made as simple as possible, but not one bit simpler.
by Albert Einstein

PhysX Preview of Cloth, Fluid and Soft Body

A8.47.1P
Re: for loop with blank comparison statement [Re: rojart] #358936
02/14/11 14:27
02/14/11 14:27
Joined: Jul 2000
Posts: 27,987
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,987
Frankfurt
Yes, it should. But indeed, a missing comparison expression causes the iterator expression not to be executed. This will be fixed. Until then, instead of omitting the comparison, insert '1' as suggested. Then the loop should behave as in standard C.

Page 2 of 2 1 2

Moderated by  aztec, Inestical, Matt_Coles, Tobias 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1