Loops?

Game Editor comments and discussion.

Loops?

Postby Toasterman » Wed Aug 18, 2010 5:15 pm

Hello, can anyone explain loops to me? ("for", "do while", "when" etc.)

I've seen plenty of examples of loops, but dont really get what they are suppose to do or how they work

like this one:

int i,j;
for(i=0;i<j;i++)
{
//action here
}

also: what is the purpose of loops? is it somthing you can easily go without or are they nessessary?

sorry if this is explained elsewhere, i didnt find anything via the search
"If there are no stupid questions, what kind of questions do stupid people ask? Do they get smart just in time to ask a question? -Scott Adams

Hey! I've got okayish internet now! Now I can try all those cool demos I've seen!
User avatar
Toasterman
 
Posts: 75
Joined: Thu Jul 22, 2010 4:22 am
Location: Drowning in Dial-Up Ocean
Score: 7 Give a positive score

Re: Loops?

Postby krenisis » Wed Aug 18, 2010 9:34 pm

if(jump==1)

velocity=-15;
jump=0;

If thats what your taking about then I can explain. Basically when variables activated by a button or event , the loop will reset the value of the variable after the action has taken place.
Ok to simplify it
Jump: is the variable

Yvelocity=-15; means your actor will jump this high

jump=0; : Means you will not be able to multi-jump. After you jump you will land on the ground where the jump will be reset.
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

Re: Loops?

Postby DST » Wed Aug 18, 2010 9:40 pm

Loops are one of the most important things you'll ever use in programming; they allow you to automate tasks, which is one of the core components of software.

For instance, you can search and sort lists; create or modify actors; and perform repetitive tasks with only a few lines of code.

Code: Select all
int i;
setpen(255, 0, 0, 0, 2);
for(i=0; i<24; i++){
moveto(i*32, 0);
lineto(i*32, height);
moveto(0, i*32);
lineto(width, i*32);
}

The above code will draw a grid of lines across the canvas; instead of requiring 24 (moveto) and 24 (lineto) codes, the entire grid takes only 8 lines of code.



Toasterman wrote:
int i,j;
for(i=0;i<j;i++)
{
//action here
}



declare integer i. declare integer j. (you must declare temporary variables before you can use them.)

i starts at 0(i=0;). As long as i is smaller than j(i<j), perform loop. increase i by one each time(i++).

another is while;
Code: Select all
int i, j;
while (i<2){
//do something;
j++;
if(j==10){
j=0;
i++;}
}


The second one will perform the action as long as the statement following 'while' is true; In this one, simple repetitions aren't enough to stop the loop, as they are in a for loop; the loop continues until the condition is no longer true. In this case, j must reach 10 twice before i is no longer <2.

Be careful with while loops! If the condition never reaches false, the loop will continue forever, effectively freezing your program!

For loops are safer. Do loop is just another way to write a while loop.

From spawning spread shots to autotargeting to creating explosions and sparks, loops make writing code 100x easier.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Loops?

Postby Toasterman » Wed Aug 18, 2010 10:27 pm

thank you both! :D very helpful

by the way, why "i" and "j"? do they stand for something, or are they just generic variables?
"If there are no stupid questions, what kind of questions do stupid people ask? Do they get smart just in time to ask a question? -Scott Adams

Hey! I've got okayish internet now! Now I can try all those cool demos I've seen!
User avatar
Toasterman
 
Posts: 75
Joined: Thu Jul 22, 2010 4:22 am
Location: Drowning in Dial-Up Ocean
Score: 7 Give a positive score

Re: Loops?

Postby DST » Wed Aug 18, 2010 11:06 pm

They are just generic variables.

Typically, programmers use i for loop counters; i guess it stands for integer.
Then j is just used because it's the next letter.

It's a good habit to get into because when viewing other's code, or letting them view yours, the code all works the same, all using i for loops.

You can declare any variable of any type in that way; even variables that you can't declare in ge variables tab. But they only stay for that one script, and then they disappear. However, you can call them in a global script, and they'll stick around the entire game.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Loops?

Postby krenisis » Thu Aug 19, 2010 12:09 am

If you want to view a simple loop , download my pong game , the one with the GED file in the demo section.
I will explain here what the loop does.
In pong I wanted the enemy paddle will respond to where the ball is moving. Effectively giving the enemy paddle AI. In order to get this effect , I have 2 loops that will make the enemy paddle follow the ball, then reset and go the other direct when needed.
Tutorial Database for all beginners click this link
viewtopic.php?f=4&t=8680
krenisis
 
Posts: 606
Joined: Sat Jul 25, 2009 3:27 pm
Score: 51 Give a positive score

Re: Loops?

Postby DST » Thu Aug 19, 2010 12:14 am

A programming loop is not the same as a logical loop.

if(x>300){x=0;} is a logical loop, using an if statement.

A logical loop would usually loop a variable. A programming loop loops code.

Programming loops are used to automate processes, assign and retrieve large amounts of variables, and search lists/perform mathematical calculations. A programming loop is often used to automate logical loops.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest

cron