Data Realms Fan Forums http://45.55.195.193/ |
|
What exactly is "i"? http://45.55.195.193/viewtopic.php?f=73&t=30632 |
Page 1 of 1 |
Author: | ryry1237 [ Wed Mar 07, 2012 4:16 am ] |
Post subject: | 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. |
Author: | CaveCricket48 [ Wed Mar 07, 2012 5:39 am ] |
Post subject: | 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. |
Author: | ryry1237 [ Wed Mar 07, 2012 8:40 am ] |
Post subject: | 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? |
Author: | CaveCricket48 [ Wed Mar 07, 2012 12:25 pm ] |
Post subject: | Re: What exactly is "i"? |
Yep. |
Author: | Duh102 [ Wed Mar 07, 2012 4:55 pm ] |
Post subject: | 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. |
Author: | HoneyFox [ Thu Mar 08, 2012 12:16 pm ] |
Post subject: | 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... |
Author: | robolee [ Sun Mar 25, 2012 11:30 pm ] |
Post subject: | 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. |
Author: | Geti [ Mon Mar 26, 2012 8:51 am ] |
Post subject: | 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. |
Page 1 of 1 | All times are UTC [ DST ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |