- For loops are code that cycles until the "for" statement is no longer true.
- It's formatted as
- for (var variable = #; i < #; increment) {
code;
} - for (var i = 4; i < 24; i++) {
console.log(i);
} - The for loop first looks at the variable and if it applies to the parameter after it then it will apply the increment to the variable and then run the code. It will continue to do this until the for loop is false. In other words, for the code above, the variable i is equal to 4 and as long as it is less than 24 it will increase by 1 and print its current value making the output 4-23
- arrays can store multiple values and its elements start with 0
- they are declared like
- var junkData = ["Eddie Murphy", 49, "peanuts", 31];
- [element 0] [1] [2] [3]
http://students.gctaa.net/~snicholakos/javascript/search_string.js
The JavaScript for loop, which is taken directly from the C for loop (and is thus shared with C++, Java, C#, and several other languages) is a bit more general (and less intelligent -- loops don't really "look at variables" ;-) than you describe.
ReplyDeleteI've written a little example loop that illustrates this point. I put it in the javascript directory of your website. Run it, study its output, and see if you can write a more accurate description of how for loops work. Don't hesitate to ask me if you have any questions.