Explain "For" statements

So I’ve been looking around, asking people, and I haven’t gotten a clear answer. What are for statments (Each part) and what do they do?

Here is an example I just copied off a script on here.

for (int i = 1; i < 31; i++)

What does this mean and what can it be used for? Thanks!

I am no master at for statements but what they do is run a series of checks essentially.
What this means, in your example is…

for… int (interger value, no decimal) = 1… so starting at 1.
as long as i is < 31… go into the method.
So…
into the method could be… look at all game objects with the tag.player
and reset them to x position.
continue to do this until i = 31.

helps?

It’s a basic loop. That is what it’s called: a for loop.

for (int i = 1; i < 31; i++)
{
… do stuff
}

This in english would read like:

loop from 1 to 30.

In code, what is happening is we make a variable called i, and for as long as it is less than 31, we add 1 to i. Obviously, when it it is no longer less than 31 ( <31 ), the loop will terminate.

In C#: for (int i = 1; i < 31; i++)

In javascript: for (var i:int = 1; i<31; i++)

In basic: for i = 1 to 30

In english: loop 30 times, starting with 1

There are many ways to use for loops. Backwards, forwards, multiplied and so on, but the above example is the most common. i usually is just called i because it used to mostly mean the index of an array. You could call it poo if you wanted to:

for (var poo:int = 1; poo<31; poo++)

var poo:int = 1; creates the variable poo and sets it to 1 (in js).

poo<31; check to see if poo is still under 31, if it is then do the following line:

poo++; this means add 1 to poo.

I hope thats clearer. You’ll be best off getting a proper book on c# programming for beginners.

is it true that while running a for loop that
a. no processing goes on in the entire program outside this statement?
b. no programming goes on in this script while running this statement?
c. no programming goes on inside this function while running this statement?
d. none of the above?

A, B and C renman. Its essentially the same as copying and pasting it however many times you run through the loop. Its going to go through the loop until its done and then its going to continue on. It would treat a for to loop that loops through itself a dozen times the same as writing out a dozen print statements individually.

So, can it be said that when you use a for statement that the program, for lack of a better explanation stops ALL OTHER operations?

Not exactly, it just does it before going on. For example, using hippocoders line from above.

for (int i = 1; i < 31; i++)
{
print (i);
}

print(“Done!”);

would print i 31 times over, raising its value by one before it would print done. It’s doing the same thing as something like this.

print(“1”);
print(“2”);
and so on until you reached 31. The fact that it does this is never a problem unless your for to loop gets incredibly massive.

edit: fixed a minor typo.

Right so it just needs to perform its task.

That being said, while say i have two Update functions running on two scripts. Can only one line be read at a time?

The best scripting lesson on the internet i found to learn C# or other is writting the following into google :

http://lmgtfy.com/?q=C%23+for#seen

Basically if you writte C# and another word, you will each time find an URL starting with Msd.microsft.com .

In your case try writting “C# for” and you will find an amazing explanation with extra exemple and resource!!:slight_smile:

LMGTFY was one of my favorites for that kind of questions ^^

Well, if you read my OP, you’d see that I have, and no one really explained it quite as well as Hippo did. Thank you Hippo! (And everyone else that tried helping me)