i have a javascript question - Post...
here's my code:
<script>
for (j = 0 ; j < 17 ; ++j)
{
x = j
document.write(x)
document.write(' ')
document.write(j)
document.write('<br />')
}
</script>
I do not understand why I get 0 0 on the first line of my html page when i am asking the for loop to pre increment j. Can anyone help me understand? I appreciate it!!!
<script>
for (j = 0 ; j < 17 ; ++j)
{
x = j
document.write(x)
document.write(' ')
document.write(j)
document.write('<br />')
}
</script>
I do not understand why I get 0 0 on the first line of my html page when i am asking the for loop to pre increment j. Can anyone help me understand? I appreciate it!!!
If memory serves with my looping mechanisms, while you have the loop condition set up to a pre-increment, the for loop won't evaluate the incrementing statement (++j) until after the first time the loop has run.
If you do want to update your value before processing the, what you need to use is a while loop, since it evaluates the condition before actually performing the details in the loop. When updated, it looks like this:
Now the big trick (I'm not sure if you're trying to do this or not) to having "chasing" values is to increment the x value at the end of your while loop:
Hope this helps!
If you do want to update your value before processing the, what you need to use is a while loop, since it evaluates the condition before actually performing the details in the loop. When updated, it looks like this:
<script type="text/javascript">
var j = 0;
while (++j < 17)
{
x = j;
document.write(x);
document.write(' ');
document.write(j);
document.write('<br />');
}
</script>
var j = 0;
while (++j < 17)
{
x = j;
document.write(x);
document.write(' ');
document.write(j);
document.write('<br />');
}
</script>
Now the big trick (I'm not sure if you're trying to do this or not) to having "chasing" values is to increment the x value at the end of your while loop:
<script type="text/javascript">
var x = 0;
var j = 0;
while (++j < 17)
{
document.write(x);
document.write(' ');
document.write(j);
document.write('<br />');
x=j;
}
</script>
var x = 0;
var j = 0;
while (++j < 17)
{
document.write(x);
document.write(' ');
document.write(j);
document.write('<br />');
x=j;
}
</script>
Hope this helps!
http://about.me/dillieo
Code slinger... Egg hunter extraordinaire...
Code slinger... Egg hunter extraordinaire...
thanks!
To explain your first statement to help in the future
If you set j=1 in your first statement that would give you a start of 1.
You get 0 0 as a result due to this:
document.write(x) = 0 because J= 0 and you are saying x = j
document.write(' ') one space
document.write(j)= 0 as set
if J were set to 1 initially it would be 1 1
Programming employs the rules of algebra and applies to all programming languages.
If you set j=1 in your first statement that would give you a start of 1.
You get 0 0 as a result due to this:
document.write(x) = 0 because J= 0 and you are saying x = j
document.write(' ') one space
document.write(j)= 0 as set
if J were set to 1 initially it would be 1 1
Programming employs the rules of algebra and applies to all programming languages.
The Guy from OZ
Prism - just to be clear, I wasn't questioning basic algebra.
I was questioning why the preincrement ++j does not preincrement.
There is no difference in the behavior of a for loop whether i preincrement with a ++j or post increment with a j++ as far as I can tell.
I was questioning why the preincrement ++j does not preincrement.
There is no difference in the behavior of a for loop whether i preincrement with a ++j or post increment with a j++ as far as I can tell.
I was questioning why the preincrement ++j does not preincrement.
Why should it you have told the expression to start with zero so x also equals 0
The important part is you have said start with a value of j = 0 then the next time round increment j by 1
for (j = 0 ; j < 17 ; ++j)
{
x = j
If j= 1 then the starting value would be 1. Nothing to do with the increment until it reiterates. Read my explanation again you are asking the value of j to be 0 and that is what you get.

Or
s = 1 The starting value or whatever you want it to be. It will be that value until it loops
for(j=s;j<17;++j)
++ is not a pre increment it is just an increment
The Guy from OZ
i'm not sure why you say ++j is not a pre increment. in any chunk of code other than a for loop, it is absolutely a pre increment, and j++ is a post increment. They do different things.
Php is similar to Javascript I changed the variables to conform and 5 instead of 17 to keep the progression small
Result :- 0 0 1 1 2 2 3 3 4 4
Changing the increment to $j++
<?php
for ($j = 0 ; $j < 5 ; $j++){
$x=$j;
Echo $j." ".$x." ";
}
?>
Result :- 0 0 1 1 2 2 3 3 4 4
Changing the value of $j=1
Result :- 1 1 2 2 3 3 4 4
<?php
for ($j = 0 ; $j < 5 ; ++$j){
$x=$j;
Echo $j." ".$x." ";
}
?>
for ($j = 0 ; $j < 5 ; ++$j){
$x=$j;
Echo $j." ".$x." ";
}
?>
Result :- 0 0 1 1 2 2 3 3 4 4
Changing the increment to $j++
<?php
for ($j = 0 ; $j < 5 ; $j++){
$x=$j;
Echo $j." ".$x." ";
}
?>
Result :- 0 0 1 1 2 2 3 3 4 4
Changing the value of $j=1
<?php
for ($j =1 ; $j < 5 ; $j++){
$x=$j;
Echo $j." ".$x." ";
}
?>
for ($j =1 ; $j < 5 ; $j++){
$x=$j;
Echo $j." ".$x." ";
}
?>
Result :- 1 1 2 2 3 3 4 4
The Guy from OZ
I completely agree. That is all true. That is exactly what I said.
Have something to add? We’d love to hear it!
You must have an account to participate. Please Sign In Here, then join the conversation.