Page 1 of 1

How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 12:28 pm
by Camper1995
Hello guys. I've been thinking how to use for function. I can't find it in GE Documentation and I cant find some explaination on forum too.

You guys are very often writing this:
(just an example)
Code: Select all
int i;
for (i=3;i<100;i++)


I understand, you made variable "i" and you put it in "for cycle". But how to use it? Can someone explain me how to use it?

Thank you. :roll:

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 1:26 pm
by lcl
It's useful for checking things.
Let's say you have an array size 10. Like, array[10] and you want to check if
some value of array is equal to 5. Now you can use for.
Code: Select all
int i;
for (i = 0; i < 10; i ++)
{
    if (array[i] == 5)
    {
        //action here
    }
}

Did that make sense? :D

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 2:04 pm
by Camper1995
Oooh great! Hehe. Awesome. Thank you man! :twisted:

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 2:41 pm
by lcl
Edit: post removed xD

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 2:45 pm
by lcl
It took me also quite long time to understand what it does. :)
It is very useful thing for coding..
Think about that my example if the array was size of 100.

If you had done it without using for, it would have been something like:
Code: Select all
if (array[0] == 5 || array[1] == 5 || ...)

Very much work! :lol:

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 3:27 pm
by Camper1995
indeed. :) Thanks, I'll try something ;)

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 3:33 pm
by Hblade
Code: Select all
int i;
for (i=0;i<100;i++) {
do loop here
}


i=0 is the default value i starts with
i<100 means, if its less than 100
i++ adds i by 1

The loop all happens in 1 frame, so it will loop or do what ever you just told it to do 100 times in 1 frame.

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 3:42 pm
by lcl
Or that's how it should work..
It gets slow sometimes, if you do much things.
Like my random level generator.
If you make it to create something like 5000 platforms it takes longer than 1 frame.. xD

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 3:47 pm
by Hblade
Yeah :D LCL I know you explained it already so I gave you a point :) I didnt see your post, sorry about that xD

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 3:54 pm
by lcl
Hblade wrote:Yeah :D LCL I know you explained it already so I gave you a point :) I didnt see your post, sorry about that xD

xD :lol:

Re: How to use "for"? Need help.

PostPosted: Fri Mar 11, 2011 3:57 pm
by Game A Gogo
lcl wrote:Or that's how it should work..
It gets slow sometimes, if you do much things.
Like my random level generator.
If you make it to create something like 5000 platforms it takes longer than 1 frame.. xD

Wrong! The frame just takes longer to process, because the CPU is overloaded. But it will still remain a frame, and everything else will "pause" for that process to finish. If you wanted to the loop to be produced over several frames, it'd make it less laggy but maybe more "choppy". An exemple that I could state would be my P2040 level editor where it draws a line of tiles per frame instead of the whole thing at once.

Re: How to use "for"? Need help.

PostPosted: Sat Mar 12, 2011 11:51 am
by Camper1995
Thank you guys! Now I understand it a bit better! ;- ))

Re: How to use "for"? Need help.

PostPosted: Sat Mar 12, 2011 12:27 pm
by Game A Gogo
No problem! like Jazz_e_bob always said in his signature: "Controlling complexity is the essence of computer programming." thus understanding how your code gets executed is a must

Re: How to use "for"? Need help.

PostPosted: Sat Mar 12, 2011 2:09 pm
by lcl
Game A Gogo wrote:
lcl wrote:Or that's how it should work..
It gets slow sometimes, if you do much things.
Like my random level generator.
If you make it to create something like 5000 platforms it takes longer than 1 frame.. xD

Wrong! The frame just takes longer to process, because the CPU is overloaded. But it will still remain a frame, and everything else will "pause" for that process to finish. If you wanted to the loop to be produced over several frames, it'd make it less laggy but maybe more "choppy". An exemple that I could state would be my P2040 level editor where it draws a line of tiles per frame instead of the whole thing at once.

Ah.. you're right! That makes sense! :)