FRIHOSTFORUMSFAQTOSBLOGSDIRECTORY
You are invited to Log in or Register a Frihost Account!

Javascript loops

 


bladesage
Okay, I've got the coolest book on javascript a few months ago. Except while it does briefly describe loops, it doesn't explain how they work or how to really take advantage of them.

It only really does say how to validate an e-mail address with them, which I don't have much use for at all. I need to know, in plain English, how to initiate a loop, what they're good for, how I would stop a loop, what and how I can have them do (ie: perform a function, check the values of variables, etc), and how they work.

Also, could I use a loop to search for a string of text in an array of other strings (like to search for a word in a list of topics or something)? All this info would be great.

Or would it be easier to do this kind of stuff in php?
Ranfaroth
Are you talking about timers ? If so, read this doc for example.
Or are you talking about statements ? (for, while...)
bladesage
I'm looking for "for/in loops" as I think they're called. The book says something like "for(...){" and so on.
LukeakaDanish
PHP and javascripts loops are almost identical...at a basic level anyway...

I recomend the do {} while() loops instead of the for loops...basically because i find them less confusing and easier to understand.

Here is how they work: (i've used your "string" question as an example)

Code:


//firstly we initiate some variables...

searchstring = "I am a n00b";
stringArray = new Array ("I am 1337","I am a fool","I have skillz","I am a n00b","my name is lukeakadanish");
var answer;

//then we initiate a COUNTER...its common practice to use the letter i, unless your already using it, in which case u use j then k, l, m, n etc.

i = 0;

//(remeber that arrays start at zero)

//now we start the loop.

do {
      //here we do what we want to do
      if (searchstring == stringArray[i]) {
            answer = i;
      }
      //Then we do whatis called the "increment"...keep reading and you will hopefully understand why this is ESSENTIAL and must NEVER be forgotten!
      i++;
}
while(i<stringArray.length);

//This tells Javascript to do whats inside the "do" section again, if our COUNTER (i) is less than the length of the Array (how many strings are in it)...if it isnt less than the length of the array then we have reached the end of the array and the loop ends




Hope you understand that...if not...please ask me about anything there.

PS: My single line comments seem to take up rather a lot more than one line...they should all be removed or the script ma appear not to work...
rohan2kool
Using the for loop saves you some memory in many cases, in most of them it doesn't matter what you use.

Basically, a loop is a repeated code block several times over and over again, till the time a given condition is satisfied. Let me give you an example of a while loop.

Code:

var i = 0;

while(i<5) {
   alert("Value of i: " + i);
}


Now, this is an infinite loop. Becuase the value of i is forever 0 and hence it is always less than 5. Hence on computing the logical statement, i<5, it is computed to TRUE. The while loop continues till the condition remains TRUE. Since it always remains true in this example, it is an ifinite loop.

Always, programming has a purpose. Similarly loops are executed for some purpose. Infinite loops are never created intentionally. Variables present in the condition are modified within the loop. Take this example:

Code:

var i = 0;

while(i<5) {
  alert("Value of i: " + i);
  i++;
}


It will run for 5 times, after which the value of i will become 5. Since, i is now not smaller than 5, the logical statement, i<5 will compute to a FALSE.Since it returns a FALSE, the loop will quit. It is as simple as that. Now, let me tell you a for loop and it's advantages.

A for loop consists of three parts: 1. the initializer, 2. the condition and 3. the incremental. The syntax is:

for(initialiser; condition; incremental) {
//code block
}

Code:

for(var i=0; i<5; i++) {
  alert("Value of i: " + 50;
}


The initialiser is what happens the time the loop is initiated or started. In our case, when we declare the for loop, the variable called 'i' is declared and set the value of 0.

The condition here is same as that in the while loop. The condition should always return TRUE for the loop to run.

The incremental is what happens everytime the loop is repeated. In our case we are increasing the value of i by 1.

The only advantage of for loop is that: 1. It is a good way of writing code. 2. It saves you some memory as the variable 'i' is stored in the memory only while the loop runs, while in the 'while' loop example, it is there till the outer part, maybe a function or the main program in whatever the loop was contained is running.

Loops are frequently used for accessing arrays. For example(in php):

Code:

$myvar = array('apple', 'banana', 'strawberry', 'orange', 'grape');
for($i=0; $i<5; i++) {
  printf("The fruit at location ".$i." is ".$myvar[$i]);
}


Remember, the variable $i is always accesible in it's modified form within the loop. I hope this explaination helps you. In case of doubt do ask.
Reply to topic    Frihost Forum Index -> Scripting -> Html, CSS and Javascript

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2007 Frihost, forums powered by phpBB.