Using equality in 'for' loop - why not? | | |
A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
must equate as true or false
This doesn't work...
x = 5;
for (y=1; (y==5); y+=1) {
alert(x * y);
}
...nor does...
x = 5;
for (y=1; (y===5); y+=1) {
alert(x * y);
}
....but this does..
x = 5;
for (y=1; (y<6); y+=1) {
alert(x * y);
}
Why do the first two fail? If i is value 5 then i==5 is true, as is
i===5.
Can anyone explain what I'm missing here?
Regards | | | | re: Using equality in 'for' loop - why not?
In article <c97mqu$ihk$1$8300dec7@news.demon.co.uk>, Mark Anderson
says...[color=blue]
> A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
> must equate as true or false
>
> This doesn't work...
> x = 5;
> for (y=1; (y==5); y+=1) {
> alert(x * y);
> }[/color]
This fails because after the first loop "y" becomes 12. Try
for (y=1; y!=5; y++)
[color=blue]
> Why do the first two fail? If i is value 5 then i==5 is true, as is
> i===5.[/color]
The loop executes while the condition is true. In your example you
assign 1 to y, so the condition y==5 is false.
[color=blue]
> Can anyone explain what I'm missing here?[/color]
You're missing the documentation: http://tinyurl.com/jbie
--
Hywel I do not eat quiche http://kibo.org.uk/ http://kibo.org.uk/mfaq.php | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote:
Hi Mark
[color=blue]
> A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
> must equate as true or false
>
> This doesn't work...
> x = 5;
> for (y=1; (y==5); y+=1) {
> alert(x * y);
> }[/color]
yes, I think it works.
Just not as expected.
Your 'test' fails the first time because y=1 and not 5.
Think of for-loop as follows, maybe that helps:
for (initialize; test; increment){
statements;
}
is equal to:
initialize;
while(test) {
statements;
increment;
}
Now you can explain the rest too. :-)
[color=blue]
> ..nor does...
> x = 5;
> for (y=1; (y===5); y+=1) {
> alert(x * y);
> }
> ...but this does..
> x = 5;
> for (y=1; (y<6); y+=1) {
> alert(x * y);
> }
>
> Why do the first two fail? If i is value 5 then i==5 is true, as is
> i===5.
>
> Can anyone explain what I'm missing here?
>
> Regards[/color]
Regards,
Erwin Moller | | | | re: Using equality in 'for' loop - why not?
Mark Anderson said:[color=blue]
>
>A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
>must equate as true or false
>
>This doesn't work...
>x = 5;
>for (y=1; (y==5); y+=1) {
> alert(x * y);
>}
>..nor does...
>x = 5;
>for (y=1; (y===5); y+=1) {
> alert(x * y);
>}
>...but this does..
>x = 5;
>for (y=1; (y<6); y+=1) {
> alert(x * y);
>}
>
>Why do the first two fail? If i is value 5 then i==5 is true, as is
>i===5.
>
>Can anyone explain what I'm missing here?[/color]
You're setting x=5 and y=1 and seem to be surprised that y!=5.
Without knowing you, it's hard to guess whether this is oversight
or if you really are missing something. | | | | re: Using equality in 'for' loop - why not?
"Hywel" <hyweljenkins@hotmail.com> wrote in message
news:MPG.1b2172c03e6af95e989727@news.individual.ne t...[color=blue]
> In article <c97mqu$ihk$1$8300dec7@news.demon.co.uk>, Mark Anderson
> says...[color=green]
> > A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
> > must equate as true or false
> >
> > This doesn't work...
> > x = 5;
> > for (y=1; (y==5); y+=1) {
> > alert(x * y);
> > }[/color]
>
> This fails because after the first loop "y" becomes 12.[/color]
Why is the first iteration performed and how does 'y' become 12?
--
S.C. | | | | re: Using equality in 'for' loop - why not?
Stephen Chalmers wrote:
[color=blue]
> Why is the first iteration performed and how does 'y' become 12?
>[/color]
x = 5;
for (y=1; y==5; y+=1) {
alert(x * y);
}[color=blue]
> S.C.[/color]
It doesn't.
The loop never starts, since y doesn't equal 5.
Mick | | | | re: Using equality in 'for' loop - why not?
Lee
"Lee" <REM0VElbspamtrap@cox.net> wrote in message
news:c97pch02pu7@drn.newsguy.com...[color=blue]
> Mark Anderson said:[color=green]
> >
> >A 'for' loop takes 3 arguments (initialize; test; increment). The[/color][/color]
'test'[color=blue][color=green]
> >must equate as true or false
> >
> >This doesn't work...
> >x = 5;
> >for (y=1; (y==5); y+=1) {
> > alert(x * y);
> >}
> >..nor does...
> >x = 5;
> >for (y=1; (y===5); y+=1) {
> > alert(x * y);
> >}
> >...but this does..
> >x = 5;
> >for (y=1; (y<6); y+=1) {
> > alert(x * y);
> >}
> >
> >Why do the first two fail? If i is value 5 then i==5 is true, as is
> >i===5.
> >
> >Can anyone explain what I'm missing here?[/color]
>[/color]
[color=blue]
> Without knowing you, it's hard to guess whether this is oversight
> or if you really are missing something.
>[/color]
[color=blue]
> You're setting x=5 and y=1 and seem to be surprised that y!=5.[/color]
And so I should be! I'm saying "start with y=1 ad while y is not 5
(ergo less than or equal to 5) do the loop and increment by five.
Your explanation is implying I'm testing in loop #1...
x = 5;
for (y=5; (y===5); etc...
I feel oppressed by all the right-brain thinking here.
Why didn't the blokes (perhaps ladies?) who wrote the spec just say
explicitly don't use ==/!=/===/!== in for loop test if they don't work.
I've looked at the NS docs - and there's no indication that these aren't
allowed.
Please folks I'm not an engineer or a logic PhD. Could posters please
*explain* their put-downs rather than just say you're wrong. I asked the
question both because I don't understand and I wish to understand why.
So far all that I've read is "I'm cleverer than you" answers telling me
I'm wrong. I know! Just please explain why...
Regards
Mark | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote:[color=blue]
> And so I should be! I'm saying "start with y=1 ad while y is not 5
> (ergo less than or equal to 5) do the loop and increment by five.[/color]
You set y to 1 and then loop while y is equal to 5. Since y is not equal
to 5 before the first iteration, the loop body is never executed.
[color=blue]
> Why didn't the blokes (perhaps ladies?) who wrote the spec just say
> explicitly don't use ==/!=/===/!== in for loop test if they don't work.
> I've looked at the NS docs - and there's no indication that these aren't
> allowed.[/color]
They are allowed, and there are cases where they make sense, for example
for (counter = 0; stillrunning == 1; counter++) {
if (something) stillrunning = 0;
}
The condition can be more complex, too -- in any case the way the loop
is processed is such that the condition is checked *before* executing
the loop, so if your condition is not true to start with, the loop is
executed zero times.
Hope that helps
--
Klaus Johannes Rusch KlausRusch@atmedia.net http://www.atmedia.net/KlausRusch/ | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote on 28 mei 2004 in comp.lang.javascript:[color=blue]
> This doesn't work...
> x = 5;
> for (y=1; (y==5); y+=1) {
> alert(x * y);
>}[/color]
Yes it works
start with y=1
then do something WHILE y==5
WHICH boolean is not true from the start,
so no alert output !!!
[color=blue]
> ..nor does...
> x = 5;
> for (y=1; (y===5); y+=1) {
> alert(x * y);
>}[/color]
Yes it works
start with y=1
then do something WHILE y===5
WHICH boolean is NOT true from the start,
so no alert output !!!
[color=blue]
> ...but this does..
> x = 5;
> for (y=1; (y<6); y+=1) {
> alert(x * y);
>}[/color]
Yes it surely works
start with y=1
then do something WHILE y<6
WHICH boolean is true when y=1, 2, 3, 4, 5
So 5 times an alert output
====================
btw:
1 the () around y<6 are not needed
2 y+=1 is the same as y++
3 alerts in a loop can drive you crazy
4 using var can sometimes save you trouble when in a function,
because it leaves global values alone
Commonly this is used:
var x = 5;
for (var y=1; y<6; y++) {
document.write( x*y + "<br>");
}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress) | | | | re: Using equality in 'for' loop - why not?
Klaus Johannes Rusch wrote on 28 mei 2004 in comp.lang.javascript:
[color=blue]
> They are allowed, and there are cases where they make sense, for example
>
> for (counter = 0; stillrunning == 1; counter++) {
> if (something) stillrunning = 0;
>}[/color]
No, no sense, but this does:
var stillrunning = 17
for (counter = 0; stillrunning == 1; counter++) {
if (counter==2300 or somethingelse) stillrunning = 1;
}
though I would prefer:
var stillrunning = true
for (counter = 0; stillrunning ; counter++) {
if (counter==2301 or somethingelse) stillrunning = false;
}
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress) | | | | re: Using equality in 'for' loop - why not?
Stephen,
"Stephen Chalmers" <me@here.com> wrote in message
news:40b78067_2@mk-nntp-2.news.uk.tiscali.com...[color=blue]
>
> "Hywel" <hyweljenkins@hotmail.com> wrote in message
> news:MPG.1b2172c03e6af95e989727@news.individual.ne t...[color=green]
> > In article <c97mqu$ihk$1$8300dec7@news.demon.co.uk>, Mark Anderson
> > says...[color=darkred]
> > > A 'for' loop takes 3 arguments (initialize; test; increment). The[/color][/color][/color]
'test'[color=blue][color=green][color=darkred]
> > > must equate as true or false
> > >
> > > This doesn't work...
> > > x = 5;
> > > for (y=1; (y==5); y+=1) {
> > > alert(x * y);
> > > }[/color]
> >
> > This fails because after the first loop "y" becomes 12.[/color]
>
> Why is the first iteration performed and how does 'y' become 12?
>
> --
> S.C.[/color]
Wait a minute, a 'for' loop excutes *until* test it true. Ergo, on
iteration #1, i=1 and test is 'false' and lop #1 executes - except it
doesn't. Can you please prove y=12 rather than state such a provactive
aasertion without explanation. I posted here to learn not be told I'm
too dumb to understand.
I don't mind rules as long as they can be understood. It seems the logic
on the for loop rests on some abstruse logic that - self-evidently -
isn't obvious.
Even so I'm a pragmatist and I'll go with the flow but it's a shame no
JS book or online tutorial I can find says not to use == or === (or
their negative equivalents) in a for loop as the test.
Regards
Mark | | | | re: Using equality in 'for' loop - why not?
Evertjan,
"Evertjan." <exjxw.hannivoort@interxnl.net> wrote in message
news:Xns94F7DE8466340eejj99@194.109.133.29...[color=blue]
> Mark Anderson wrote on 28 mei 2004 in comp.lang.javascript:[color=green]
> > This doesn't work...
> > x = 5;
> > for (y=1; (y==5); y+=1) {
> > alert(x * y);
> >}[/color]
>
> Yes it works
>
> start with y=1
> then do something WHILE y==5
>
> WHICH boolean is not true from the start,
> so no alert output !!!
>[color=green]
> > ..nor does...
> > x = 5;
> > for (y=1; (y===5); y+=1) {
> > alert(x * y);
> >}[/color]
>
> Yes it works
>
> start with y=1
> then do something WHILE y===5
>
> WHICH boolean is NOT true from the start,
> so no alert output !!!
>[color=green]
> > ...but this does..
> > x = 5;
> > for (y=1; (y<6); y+=1) {
> > alert(x * y);
> >}[/color]
>
> Yes it surely works
>
> start with y=1
> then do something WHILE y<6
>
> WHICH boolean is true when y=1, 2, 3, 4, 5
>
> So 5 times an alert output
>
> ====================
>
> btw:
> 1 the () around y<6 are not needed
> 2 y+=1 is the same as y++
> 3 alerts in a loop can drive you crazy
> 4 using var can sometimes save you trouble when in a function,
> because it leaves global values alone
>
> Commonly this is used:
>
> var x = 5;
> for (var y=1; y<6; y++) {
> document.write( x*y + "<br>");
> }
>
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)[/color]
And the prize goes to EvertJan. Now it is explained I understand.
Shame no books tutorials stoop so low as to explain this simple fact.
I now know why I won't use ++ etc., except in the exceptional cases such
as Klaus just described.
Thanks. My faith in Usenet is restored!
Regards
Mark | | | | re: Using equality in 'for' loop - why not?
Mark Anderson said:[color=blue]
>
>Lee
>
>"Lee" <REM0VElbspamtrap@cox.net> wrote in message
>news:c97pch02pu7@drn.newsguy.com...[color=green]
>> Mark Anderson said:[color=darkred]
>> >
>> >A 'for' loop takes 3 arguments (initialize; test; increment). The[/color][/color]
>'test'[color=green][color=darkred]
>> >must equate as true or false
>> >
>> >This doesn't work...
>> >x = 5;
>> >for (y=1; (y==5); y+=1) {
>> > alert(x * y);
>> >}
>> >..nor does...
>> >x = 5;
>> >for (y=1; (y===5); y+=1) {
>> > alert(x * y);
>> >}
>> >...but this does..
>> >x = 5;
>> >for (y=1; (y<6); y+=1) {
>> > alert(x * y);
>> >}
>> >
>> >Why do the first two fail? If i is value 5 then i==5 is true, as is
>> >i===5.
>> >
>> >Can anyone explain what I'm missing here?[/color]
>>[/color]
>[color=green]
>> Without knowing you, it's hard to guess whether this is oversight
>> or if you really are missing something.
>>[/color]
>[color=green]
>> You're setting x=5 and y=1 and seem to be surprised that y!=5.[/color]
>
>And so I should be! I'm saying "start with y=1 ad while y is not 5
>(ergo less than or equal to 5) do the loop and increment by five.[/color]
No, you've got the sense of the test wrong, and possibly
misunderstand the increment (did you mean to say "increment by one"?).
You're saying "start with y=1 and while y *is* 5 do the loop
and increment by 1".
The loop is performed *while* the condition is true,
not *until* it is true.
If you want to start with y=1 and increment by 1 while y is
not 5, you should use:
for (y=1; y<5; y++) | | | | re: Using equality in 'for' loop - why not?
In article <40b78067_2@mk-nntp-2.news.uk.tiscali.com>, Stephen Chalmers
says...[color=blue]
>
> "Hywel" <hyweljenkins@hotmail.com> wrote in message
> news:MPG.1b2172c03e6af95e989727@news.individual.ne t...[color=green]
> > In article <c97mqu$ihk$1$8300dec7@news.demon.co.uk>, Mark Anderson
> > says...[color=darkred]
> > > A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
> > > must equate as true or false
> > >
> > > This doesn't work...
> > > x = 5;
> > > for (y=1; (y==5); y+=1) {
> > > alert(x * y);
> > > }[/color]
> >
> > This fails because after the first loop "y" becomes 12.[/color]
>
> Why is the first iteration performed and how does 'y' become 12?[/color]
My mistake - I used document.write(y) in a simpler test but forgot to
use something to separate the values.
Mark - the reason your code is wrong is simple: you're testing to see it
y is equal to 5. You're trying you get a loop to work while y is 5. As
you start your loop with y=1, it is never 5. How many people have told
you that?
If you read the JavaScript documentation at http://developer.netscape.com/ you'll see that the syntax of a for loop
is quite simple:
for ([initial-expression]; [condition]; [increment-expression]) {
statements
}
The fact that the tutorials you're used do not explicitly tell you not
to use == or === is simple: the test will never evaluate in the manner
that you need for your code to continue. The logic is quite clear. For
example, this code
for (x=0; x<5; x++)
says assign the value zero to x. While x is less than 5 increment it.
When x is equal to 5, stop.
Your code
for (y=1; (y==5); y+=1)
says assign the value one to y. While y is equal to 5, increment it.
When, and how, will y ever become 5 in your code?
I appreciate that there is a learning process involved and that it may
not be always clear, but I'm sure you'll find it becomes more obvious
the more you use JavaScript.
--
Hywel I do not eat quiche http://kibo.org.uk/ http://kibo.org.uk/mfaq.php | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote:
[color=blue]
> "Stephen Chalmers" <me@here.com> wrote in message
> news:40b78067_2@mk-nntp-2.news.uk.tiscali.com...[/color]
Please do not write attribution novels, see
<http://netmeister.org/news/learn2quote.html>
[color=blue][color=green]
>> "Hywel" [...] wrote [...][color=darkred]
>> > [...] Mark Anderson [...] says...
>> > > This doesn't work...
>> > > x = 5;
>> > > for (y=1; (y==5); y+=1) {
>> > > alert(x * y);
>> > > }
>> >
>> > This fails because after the first loop "y" becomes 12.[/color]
>>
>> Why is the first iteration performed and how does 'y' become 12?
>> [...][/color]
>
> Wait a minute, a 'for' loop excutes *until* test it true. [...][/color]
If you think of `test' as in
for (init; test; loop_stmt)
{
...
}
that is wrong. A "for" loop executes *as long as* `test'
evaluates to `true'. Such a loop is head-controlled,
terminated and thus equivalent to
init;
while (test)
{
...
loop_stmt;
}
That is not equivalent to
init;
do
{
...
loop_stmt;
}
while (test)
as you apparently think.
[color=blue]
> Ergo, on iteration #1, i=1 and test is 'false' and lop #1 executes[/color]
No, it does not, unless `test' is true, i.e. "y" evaluates to 5. If we
assume that this is only a snippet, it may execute as previous statements
may have assigned 5 to y. If we assume that this is the whole code, it
never executes, since no value has been assigned to "y" and thus its
value is `undefined' which is even with type casting not equal to 5.
PointedEars | | | | re: Using equality in 'for' loop - why not?
Thanks to everyone - really! I just wish FAQs etc. Made this clear.
Running several FAQs myself I know how things I see as 'obvious' aren't
to a newbie and there's oftenno way to guess your way to expertise.
Request for addition to the n/g's FAQ? In that vein I'd also request I'd
also suggest a note to explain the safe version of var i++. Users of the
wonderful JS Lint are told it is dangerous to use. Why, is less clear;
put your JS smarts aside and try to find an explanation online - that a
newbie understands - and more importantly, a description of what you use
instead (I believe it is i+=1 ).
Thanks again to everyone for haring, now I truly understand why the
books are so cra*p and what I was doing wrong after reading them.
Regards
Mark | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote:
[color=blue]
> Thanks to everyone - really! I just wish FAQs etc. Made this clear.
> Running several FAQs myself I know how things I see as 'obvious' aren't
> to a newbie and there's oftenno way to guess your way to expertise.
>
> Request for addition to the n/g's FAQ?[/color]
Please don't, that is well-documented behavior. RTFM, i.e. see the
ECMAScript (3) Specification, the Netscape Core JavaScript Reference(s)
and the MSDN Library. `for' is not different in ECMAScript and its
implementations from any other language of the C family. It is one
of the first things people should learn when they learn how to code,
not when they learn how to code J(ava)Script.
PointedEars | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote:
[color=blue]
> "Stephen Chalmers" <me@here.com> wrote in message
> news:40b78067_2@mk-nntp-2.news.uk.tiscali.com...[/color]
Please do not write attribution novels, see
<http://netmeister.org/news/learn2quote.html>
[color=blue][color=green]
>> "Hywel" [...] wrote [...][color=darkred]
>>> [...] Mark Anderson [...] says...
>>>> This doesn't work...
>>>> x = 5;
>>>> for (y=1; (y==5); y+=1) {
>>>> alert(x * y);
>>>> }
>>>
>>> This fails because after the first loop "y" becomes 12.[/color]
>>
>> Why is the first iteration performed and how does 'y' become 12?
>> [...][/color]
>
> Wait a minute, a 'for' loop excutes *until* test it true. [...][/color]
If you think of `test' as in
for (init; test; loop_stmt)
{
...
}
that is wrong. A "for" loop executes *as long as* `test'
evaluates to `true'. Such a loop is head-controlled,
terminated and thus equivalent to
init;
while (test)
{
...
loop_stmt;
}
That is not equivalent to
init;
do
{
...
loop_stmt;
}
while (test)
as you apparently think.
[color=blue]
> Ergo, on iteration #1, i=1 and test is 'false' and lop #1 executes[/color]
No, it does not, unless `test' is true, i.e. "y" evaluates to 5. Since 1
has been assigned to "y" previously in the initialization part, it does not
evaluate to 5, so the loop is never executed.
PointedEars | | | | re: Using equality in 'for' loop - why not?
Mark Anderson wrote:
[color=blue]
>
> Why didn't the blokes (perhaps ladies?) who wrote the spec just say
> explicitly don't use ==/!=/===/!== in for loop test if they don't work.
> I've looked at the NS docs - and there's no indication that these aren't
> allowed.
>[/color]
There are situations when you can use "== !="
y=5
for(i=0;y==5;i++){
alert(y);
if(i==2){y=4;}
}
alert(y)
Mick
[color=blue]
> Mark
>
>[/color] | | | | re: Using equality in 'for' loop - why not?
"Thomas 'PointedEars' Lahn" <PointedEars@nurfuerspam.de> wrote in message
news:40B7C721.2050107@PointedEars.de...[color=blue]
> `for' is not different in ECMAScript and its
> implementations from any other language of the C family.[/color]
Hmmm. In ECMAScript, the scope of variables declared in the initialisation
statement, is not the same as it is in C++.
--
S.C. | | | | re: Using equality in 'for' loop - why not?
Stephen Chalmers wrote:
[color=blue]
> "Thomas 'PointedEars' Lahn" <PointedEars@nurfuerspam.de> wrote in message
> news:40B7C721.2050107@PointedEars.de...[/color]
Please do not write attribution novels, see
<http://netmeister.org/news/learn2quote.html>
[color=blue][color=green]
>> `for' is not different in ECMAScript and its
>> implementations from any other language of the C family.[/color]
>
> Hmmm. In ECMAScript, the scope of variables declared in the initialisation
> statement, is not the same as it is in C++.[/color]
ACK. I was referring to the test expression, this is a C-style `for'.
PointedEars | | | | re: Using equality in 'for' loop - why not?
JRS: In article <c97mqu$ihk$1$8300dec7@news.demon.co.uk>, seen in
news:comp.lang.javascript, Mark Anderson <mark@notmeyeardley.demon.co.uk[color=blue]
> posted at Fri, 28 May 2004 16:45:50 :[/color]
[color=blue]
>A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
>must equate as true or false[/color]
ISTM that this is not a matter for the FAQ, but that it is a matter for
tutorials.
In different languages, loops are expressed in many different ways; but
ISTM that in the relevant respect there are exactly two different
classes.
For any loop, there is an initialisation, there is a change-between-
loops, and there is a condition. The condition is always equivalent to
a Boolean - that is inevitable, though Booleanisation may be implicit.
One class of loop has a termination condition; false to run.
One class of loop has a continuation condition; true to run.
The first class is exemplified by
Algol's for J := 3 step 6 until 9 // J=9
Pascal's for J := 8 down to 4 // J=4
repeat Inc(J) ; ... until J=9
(the first two could be interpreted with <=, and are probably
implemented that way; but the mind is likely to consider the condition
as "finish after J equals given value");
and the second by
Javascript's for (J=3 ; J<=9 ; J+=6)
Pascal's while J<9 do begin Inc(J) ; ... end ;
In the second, the Boolean is usually, but not always, an ordered
comparison - > >= < <=, explicit or implicit. However :
for (J=new Date();J.getMonth()==4;J=new Date()) {}
for (J=new Date();J.getMonth()!=5;J=new Date()) {}
should, ISTM, each loop until June.
The OP has been giving a termination condition where a continuation
condition is required.
There is another dichotomy, that of whether the finalisation condition
is evaluated before or after the loop body
while (new Date()<1234567890000) {}
do {} while (new Date()<1234567890000)
each of which should loop until 2009-02-13 Fri 23:31:30 GMT. IIRC, a
FORTRAN IV loop tested at the end, so the loop was always executed once;
but an Algol 60 loop tested at the beginning.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links. | | | | re: Using equality in 'for' loop - why not?
"Mark Anderson" <mark@notmeyeardley.demon.co.uk> writes:
[color=blue]
> Shame no books tutorials stoop so low as to explain this simple fact.[/color]
What about :
<URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004804>
<URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jsstmfor.asp?frame=false>
[color=blue]
> I now know why I won't use ++ etc., except in the exceptional cases such
> as Klaus just described.[/color]
I always use ++, and can see no problem with that. It's just the comparison
that should usually not be an "equals-to" test.
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.' | | | | re: Using equality in 'for' loop - why not?
Lasse Reichstein Nielsen wrote:
[color=blue]
> "Mark Anderson" <mark@notmeyeardley.demon.co.uk> writes:
>[color=green]
> > Shame no books tutorials stoop so low as to explain this simple fact.[/color]
>
> What about:
> <URL:http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/stmt.html#1004804>
> <URL:http://msdn.microsoft.com/library/en-us/script56/html/js56jsstmfor.asp?frame=false>[/color]
Both references appear to be satisfactory for the description of the
for "condition".
However, the latter reference describes the "initialization",
"condition", and "increment" as being required. The former describes
the "condition" as optional, with the potential mistaken implication
that the other two are required.
In fact, the form
for (;;); // Infinite loop, by the way
is compliant with ECMA-262/3. Later browsers (don't know about earlier
ones) accept the above form, including MS IE 6, so it appears it may
be just the documentation that is not entirely "with the program". :)
../rh |  | Similar JavaScript / Ajax / DHTML bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,510 network members.
|