Jump to content

if (i<x.length-1)


Recommended Posts

Can somebody point me to where the whole i variable gets explained in detail? I've been going through all of the courses at w3schools starting from the beginning(HTML) and have made it all the way to XML, where I am now. I know in earlier sections they bring up examples like (i=x,i<x,i++) and stuff, but I just never fully understood where they are getting all that from. When I first came across it I just decided to accept it for what it was, hoping I would gain a greater understanding later on, but here I am at XML and I still don't understand fully the rules governing i and increments and such. Any help would be much appreciated.

Link to post
Share on other sites

It could be anything. It doesn't necessarily have to be called "i". It's part of Javascript's for() loop and doesn't have anything to do with XML. Perhaps you're reading the XML DOM tutorial. The prerequisite to reading this tutorial is to have read the Javascript tutorial first. Read this page, don't just glance at it or you won't get what's going on: http://www.w3schools.com/js/js_loop_for.asp

Link to post
Share on other sites

I read it thoroughly the first time. I still didn't understand it. Rereading it however I see this: Statement 1 is executed before the loop (the code block) starts.Statement 2 defines the condition for running the loop (the code block).Statement 3 is executed each time after the loop (the code block) has been executed. and it makes more sense now. This sort of clears things up for me. Thanks for linking to that specific page!

Link to post
Share on other sites
  • 2 weeks later...

the most likely reason that the variable "i" is used is because loops are typically used with arrays. each member of an array can be referenced by its index, which is what one is doing when the loop over an array, i.e.

var letters = ['a','b','c'];for(var i = 0; i < l; i++){  alert('the letter at index [' + i + '] is => ' + letters[i]);};

and remember, that arrays start at 0.

Edited by thescientist
Link to post
Share on other sites
the most likely reason that the variable "i" is used is because loops are typically used with arrays. each member of an array can be referenced by its index, which is what one is doing when the loop over an array, i.e.
var letters = ['a','b','c'];for(var i = 0; i < l; i++){  alert('the letter at index [' + i + '] is => ' + letters[i]);};

and remember, that arrays start at 0.

I believe "i" stands for "iterator"
Link to post
Share on other sites

Most of colleagues go with index. wikipedia goes as far back as the FORTRAN days and states it could be because they were integers.http://en.wikipedia.org/wiki/Loop_counter either way, it's a interesting discussion.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...