View unanswered posts | View active topics It is currently Thu Dec 26, 2024 6:09 pm



Reply to topic  [ 8 posts ] 
 What exactly is "i"? 
Author Message
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post What exactly is "i"?
I remember someone once told me that "i" was just a variable like "x" or "killtimer" or "distance" or even "derplength", yet based on how I see other people use this variable, "i" seems to almost hold some kind of magical value.

Lets say I want to use a variable called "x"
To use this variable, I must first define it as

x = 1
or x = self.Health
or x = (insert random number here)

or something like that

Yet many times I've seen people use the variable "i" without first defining what it is.

For example, in the code, they will directly write
Code:
function (Update)
for i=1 do
*insert random stuff here*
end


Nowhere in the entire code did they write anything about defining what "i" is, which means "i" could be anything, yet the code still executes correctly as if it somehow knew what "i" already is.

So this has just been getting a bit frustrating for me, since I just can't seem to understand whether there's something special about "i" or if "i" is really just a normal variable that is somehow mysteriously defined by unknown programming forces in CC.

Peace.


Wed Mar 07, 2012 4:16 am
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: What exactly is "i"?
The variable "i" works because it's part of the For loop. What the For loop does is that it loops several times and stores which iteration it is on in the variable "i," so in the first iteration "i" will be 1, next iteration i will be 2, etc.

"i" can also be replaced by any other variable name, it could be
Code:
for pancakes = 1, 10 do
    print(pancakes);
end

And everything will still run smoothly.

I'm going to guess that "i" is used because it is used to note the current iteration of the For loop.


Wed Mar 07, 2012 5:39 am
Profile
User avatar

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
Reply with quote
Post Re: What exactly is "i"?
Oh so it's not the "i" that's magical but the for loop...
*slaps face in realization*

Thanks for making it clear.
What does the 10 represent though?
Loop 10 times?


Wed Mar 07, 2012 8:40 am
Profile
User avatar

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 13144
Location: Here
Reply with quote
Post Re: What exactly is "i"?
Yep.


Wed Mar 07, 2012 12:25 pm
Profile
happy carebear mom
User avatar

Joined: Tue Mar 04, 2008 1:40 am
Posts: 7096
Location: b8bbd5
Reply with quote
Post Re: What exactly is "i"?
i is just a convenient one letter standard that most everyone understands. Somewhat like how we call the three dimensions in space x, y, and z.
If you encounter a three-nested for loop sometime in life, you may find that it uses i, j, and k, which are also parts of that standard.


Wed Mar 07, 2012 4:55 pm
Profile

Joined: Sat Dec 04, 2010 4:48 am
Posts: 13
Reply with quote
Post Re: What exactly is "i"?
"i" may come from the word 'iterator', which is an object that will enumerate all the items in a list/array/collection.
it may also come from the word 'index'. anyway, that may be a coincidence but whatever...


Thu Mar 08, 2012 12:16 pm
Profile
User avatar

Joined: Fri May 11, 2007 4:30 pm
Posts: 1040
Location: England
Reply with quote
Post Re: What exactly is "i"?
ryry1237 wrote:
What does the 10 represent though?
Loop 10 times?

contrary to what you have been told it does not mean loop ten times (except in the cases when it does). This is basically how the for loop declaration is set up:

Code:
for variable=[start value], [end value] do
    --code--
end

Basically a for loop (using 1 and 10 as in your example; for i=1,10 do) is equivalent to this:
Code:
i = 1
while i <= 10 do
    --code--
    i = i + 1
end


I think the reason it is usually written as "i" is because the variable is nothing but an iterator, it doesn't need to be descriptive and it will only be used in that one section of code; also the longer the name the more it messes up the code, example:
Code:
 thisthingyihave[i] = thisthingyihave[i-1] + someotherthingy[24-i]
compared to:
 thisthingyihave[thingynumber] = thisthingyihave[thingynumber-1] + someotherthingy[24-thingynumber]

Aint nothing but a thing.


Sun Mar 25, 2012 11:30 pm
Profile
User avatar

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
Reply with quote
Post Re: What exactly is "i"?
Also contrary to whatever reasoning you have thinking it's not defined, you're defining it with the i = 0 part. == checks equality, = is an assignment.

Code:
for i = 0, 10 do
-- ^^^ note the = sign, therefore i is assigned the value 0.
end


Just fwiw.


Mon Mar 26, 2012 8:51 am
Profile WWW
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by STSoftware for PTF.
[ Time : 0.494s | 14 Queries | GZIP : Off ]