How to use "for"? Need help.

Non-platform specific questions.

How to use "for"? Need help.

Postby Camper1995 » Fri Mar 11, 2011 12:28 pm

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:
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

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

Postby lcl » Fri Mar 11, 2011 1:26 pm

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
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

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

Postby Camper1995 » Fri Mar 11, 2011 2:04 pm

Oooh great! Hehe. Awesome. Thank you man! :twisted:
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

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

Postby lcl » Fri Mar 11, 2011 2:41 pm

Edit: post removed xD
Last edited by lcl on Fri Mar 11, 2011 2:45 pm, edited 1 time in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

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

Postby lcl » Fri Mar 11, 2011 2:45 pm

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:
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

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

Postby Camper1995 » Fri Mar 11, 2011 3:27 pm

indeed. :) Thanks, I'll try something ;)
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

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

Postby Hblade » Fri Mar 11, 2011 3:33 pm

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.
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

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

Postby lcl » Fri Mar 11, 2011 3:42 pm

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
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

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

Postby Hblade » Fri Mar 11, 2011 3:47 pm

Yeah :D LCL I know you explained it already so I gave you a point :) I didnt see your post, sorry about that xD
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

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

Postby lcl » Fri Mar 11, 2011 3:54 pm

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:
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

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

Postby Game A Gogo » Fri Mar 11, 2011 3:57 pm

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.
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

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

Postby Camper1995 » Sat Mar 12, 2011 11:51 am

Thank you guys! Now I understand it a bit better! ;- ))
Say hello to my little friend.
User avatar
Camper1995
 
Posts: 707
Joined: Tue Dec 30, 2008 7:20 pm
Location: Lost in the past.
Score: 44 Give a positive score

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

Postby Game A Gogo » Sat Mar 12, 2011 12:27 pm

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
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

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

Postby lcl » Sat Mar 12, 2011 2:09 pm

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! :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest