0
Need help...Can somebody explain me how javascript increment & decrement work in they way i can understand...
Please guys tell me more deep about javascript increment & decrement...And i just cant' understand how it works in loops...
6 Antworten
+ 2
Loops are like
for( initial counter; counter limit test; modify counter )
Where initial counter is the value of the counter the first time the loop is ran, counter limit test is the test to keep counter within a desired range, and modify counter is where you change the value of the counter for the next loop iteration (run through of the code). Contrary to how it is written the limit test is ran AFTER the modify counter step, which is run each time the loop reaches it's internal end (after calling console.log( x ); in this case). Also, because the value is modified before the loop is exited it will end at 1 modification greater than where it actually stopped at if it's a variable stored outside of the for loop.
So:
var x;
for( x = 0; // Start with x = 0
x < 5; // Run until x == 5
x++ ) { // Increase x to iterate
console.log( x );
}
console.log( "x = " + x );
Will output:
0
1
2
3
4
x=5
The algorithm used to test the limit is very important, especially if you're going to try JavaScript's multiple-counters style of for loop:
var x;
for( x = 0, y = 0;
x < 5, y <= 5;
x++, y++ ) {
console.log( x + " & " + y );
}
console.log( "x = " + x + " & y = " + y );
We will get output like:
0 & 0
1 & 1
2 & 2
3 & 3
4 & 4
5 & 5
x = 6 & y = 6
Because it's forcing x to run again since y is going to run again. If you instead change that to
var x, y;
for( x = 0, y = 0;
x < 5 && y <= 5;
x++, y++ ) {
console.log( x + " & " + y );
}
console.log( "x = " + x + " & y = " + y );
Then we would get output like:
0 & 0
1 & 1
2 & 2
3 & 3
4 & 4
x = 5 & y = 5
Because now we are making sure that both x <5 AND y <= 5. So, once either returns false (x does first, once it reaches the value of 5) it fails and the loop stops.
And just for fun a reverse loop:
var x;
for( x = 5; x > 0; x-- ) {
console.log( x );
}
console.log( "x = " + x );
Will output:
5
4
3
2
1
x = 0
+ 1
Ok i get it...Its' helps...
Thanks a lot bro Tom...
I was confused by this for days and have no idea...
+ 1
No problem :) Glad it helped. I had a lot of issues with loops in the beginning too, totally understandable.
+ 1
im new too sometimes this helps
forget the code. ask what is it doing.
its repeating a task until the condition is satisfied.
non code example: telling someone to do something a certain amount of times.
once the purpose of the command is understood all that's left is decyphering the code/syntax.
purpose why we need it. syntax how to write it.
this can be applied to command parameters or elements of a command. loop has variable, true or false test, and a counter.
these 3 things allow us to tell the machine to do something, a certain amount of times, and then stop.
a simple example, loops can do some other things too.
0
Thanks for giving another hint for me...
Hope we'll grow like loops...
0
it made sense when i wrote it ; )