Page 1 of 1

Loops?

PostPosted: Wed Aug 18, 2010 5:15 pm
by Toasterman
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

Re: Loops?

PostPosted: Wed Aug 18, 2010 9:34 pm
by krenisis
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.

Re: Loops?

PostPosted: Wed Aug 18, 2010 9:40 pm
by DST
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.

Re: Loops?

PostPosted: Wed Aug 18, 2010 10:27 pm
by Toasterman
thank you both! :D very helpful

by the way, why "i" and "j"? do they stand for something, or are they just generic variables?

Re: Loops?

PostPosted: Wed Aug 18, 2010 11:06 pm
by DST
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.

Re: Loops?

PostPosted: Thu Aug 19, 2010 12:09 am
by krenisis
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.

Re: Loops?

PostPosted: Thu Aug 19, 2010 12:14 am
by DST
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.