View Full Version : help with "for(n=2;n<=100;n++)"

02-21-2003, 07:29 PM
i have been using a script i have found to use as a basis for making a game that i played a long time ago which i really liked. ive made good progress with it but there is one thing i dont understand which has meant ive got stuck and cant get any further!

this is the bits that go for(n=2;n<=100;n++) or for(v=1;v<=100;v++). i dont understand what these mean and how they work.

what i want to do is make it so the line constantly increases. at the moment it increases for a while and then just moves along at a fixed length. also, when you die (by going into yourself or a wall) and then click on new game, i want the old line to disappear straight away and not gradually move away. ps you can move the line by using your arrow keys. i have attached the entire script for you to see.

so can someone tell me how to get the 2 things above working and what the coding i mentioned above means. if so, i would be really grateful! :) also, if anyone sees any way in which the code can be improved, let me know that too! thanking you in advance, paul

02-21-2003, 07:38 PM
I took a stab at explaining for loops here (http://www.webxpertz.net/forums/showthread.php3?s=&threadid=22365)

More from 'official' sources.

02-21-2003, 07:40 PM
Just a quick explanation of a for-loop. It's basically a compact way of writing a while-loop if that helps at all.

This line: for(n=2;n<=100;n++) creates a new variable named "n" and sets it equal to 2. Then while n is less than 100, it will perform the code in the for loop. The n++ increments the value of n every time the loop is executed.

Just try some out and see how it behaves.

The most descriptive example is the following which will print all the numbers between 2 and 100. Change the numbers around to see how it affects the output.

document.write (n)
document.write ('<br>')

Philip M
02-21-2003, 07:49 PM
You are working with a "for" loop, which is a pretty basic feature of most programming languages.

The syntax is

for (initialvalue, test, increment) statement
example:- for(v=1;v<=100;v++).

The initialvalue is the start value of the loop counter (in this case the counter is v and the initial value of v is set to 1).

At each iteration of the loop a boolean condition is tested and the next iteration will only take place if the condition (in this case "is v smaller than or equal to 100?") returns true. So the loop will execute 100 times, then stop.

v++ is the increment and is shorthand for v=v+1;, i.e. increase the value of v by 1. It would be possible to increment v by some other quantity if desired, say 5 (v=v+5).

So, the code says "execute a loop 100 times, starting with the counter (v) = 1, increasing the value of v by 1 at each pass, and going on until v reaches 100." At each pass of the loop some code is executed, that is the code contained in the statement (or statement block contained within curly braces {}).


02-21-2003, 08:06 PM
ok, i think i get it now.

ive realised that if i change the 23 bit in:
it changes the length of the line. so if i want it to go on forever, how do i do it? also, the higher this number is, the slower the line moves - which is not good!

02-21-2003, 08:33 PM
Originally posted by hypedup.co.uk
so if i want it to go on forever, how do i do it?

I don't think you want it to go on *forever* ... but this would do it:

while(i == 0) { i=0; }


Seriously though - the middle expression is the length of iteration - what you're doing there is evaluating a statement - "if this is true, then do another iteration". It doesn't have to be a fixed number - it could be another numerical propert, such as the length of an array:

for (i=0; i<ary.length; i++){
ary[i] = i;

But actually, it's inefficient to do that because you have to query the length property every iteration; much better to do this:

var aryLen = ary.length;
for (i=0; i<aryLen; i++) {
ary[i] = i;

The while loop I used before is a variant of the same thing, except that it continues for as long as the expression is true. So you could do the same loop using while, like this

var aryLen=ary.length;
var i=0;
while(i < aryLen) {
ary[i] = i;

As for speed - well yes, inevitably the more complex the loop, the longer it takes. Consider this:

for (i=0;i<64;i++){
ary[i][j] = i*j;

64 iterations of 64 iterations, but it still won't take very long because the process inside the iterations, and the evaluations themselves, are very simple. With more complex scripting, efficiency boosters (such as not querying ary.length in the expression) make a big difference.

02-21-2003, 08:49 PM
I don't think you want to use a for loop here, but a "while" or "do...while" loop. You should find information on those at the links I gave earlier.

02-21-2003, 08:58 PM
cheers for the info. its much harder than i thought it would be, its making my brain hurt! i will give it a shot, but its not looking good so far!

02-21-2003, 09:34 PM
I think this would go on *forever* (using for)

for ( var i = 0; i < i++; i++ ){
//Some Stuff Here

It increases by 1 every time, but It will never stop. Eventually the browser would realise and alert the user.

02-21-2003, 10:18 PM
ive tried but its too complicated for me to understand how to change this. :confused: ive now tried to add a second line which ive got to half-work (by duplicating everything). but this is beyond me now so unless someone can help me by telling me what needs changing and what to, it looks like im going to have to give up trying to make this game. :(

ive attached the 2 line version in case anyone does want to do me a big favour.

02-21-2003, 10:19 PM
I don't think you could get your line to go on forever as you are only creating 100 divs which also means you are limited to 100 turns

If you are going to create one div for every 5px by 5px section of the playing area, your game backbone could just depend upon testing the background colour of each referenced div.

I ran up a quick example here (http://www.teckis.com/line.html)
which alerts you with a rewarding 'yay' if you manage to cover 35% of the yellow area with red.

02-21-2003, 10:21 PM
hahaha -

on a subnote: anyone who doesn't believe that mozilla is faster at javascript than IE - just play the above linked page in both browsers.
of course, the inability of having keyboard control of the line direction does somewhat hamper you in moz.

02-22-2003, 12:01 PM
thank you Íkii, thats a nice example. i will see if i can work with that and get what i want. i see that the bigger i make the grid, the slower it all becomes. i presume this is a limitation of javascript and that i will just have to make sure that i dont make it too big?

02-22-2003, 03:47 PM
Íkii, ive been playing around with your example and have managed to create the game i wanted. you can play it at http://www.hypedup.f2s.com/games/survival.html - let me know what you think?

i had to remove the bit that cleared the lines away at the end and make the "new game" button reload the page instead as it took too long when the lines were long with your way. i would prefer it so that people didnt have to reload the page, but unless someone can think of a way that wont be too slow, i will have to leave it like this.

if anyone can see any other improvements that could be made, let me know. later on i will see if i can add a third line (to make it a 3-player game).

thanks for your help everyone (esp Íkii),

02-22-2003, 03:51 PM
Originally posted by Íkii
on a subnote: anyone who doesn't believe that mozilla is faster at javascript than IE - just play the above linked page in both browsers.
of course, the inability of having keyboard control of the line direction does somewhat hamper you in moz.

my version loads much slower in netscape 6 than with ie6! how can i make it so the keyboard control works with n6+?