View unanswered posts | View active topics It is currently Sat Oct 19, 2019 9:26 pm

 Page 1 of 1 [ 9 posts ]
 Print view Previous topic | Next topic
What exactly is "i"?
Author Message

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
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

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 11839
Location: Here
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

Joined: Sun Dec 25, 2011 7:23 am
Posts: 269
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

Joined: Tue Jun 12, 2007 11:52 pm
Posts: 11839
Location: Here
Re: What exactly is "i"?
Yep.

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

Joined: Tue Mar 04, 2008 1:40 am
Posts: 7094
Location: b8bbd5
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

Joined: Sat Dec 04, 2010 4:48 am
Posts: 13
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
Data Realms SUPER Elite

Joined: Thu Jun 03, 2010 10:06 pm
Posts: 3717
Location: Over there.
Re: What exactly is "i"?
I always thought i was used because nobody would be using it for to equal math.sqrt(-1), and thus was just known to be what it is.

Thu Mar 08, 2012 10:23 pm

Joined: Fri May 11, 2007 4:30 pm
Posts: 1040
Location: England
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

Joined: Sun Jul 13, 2008 9:57 am
Posts: 4886
Location: some compy
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
Display posts from previous:  Sort by
 Page 1 of 1 [ 9 posts ]

#### Who is online

Users browsing this forum: No registered users

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

Search for:
 Jump to:  Select a forum ------------------ Cortex Command    Dev Log    Wiki    Game Discussion    Game and Licensing Support    Mod Making       Lua Scripting       Requests    Mod Releases       Older Mods    Scene Releases       Scene Making Planetoid Pioneers    Game Discussion    Pioneer Program Lounge Data Realms Off-Topic    Off Topic Chat       Gaming       Forum Games    Site Support & Feedback