473,770 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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


Jul 23 '05
23 2211
Stephen Chalmers wrote:
"Thomas 'PointedEars' Lahn" <Po*********@nu rfuerspam.de> wrote in message
news:40******** ******@PointedE ars.de...


Please do not write attribution novels, see
<http://netmeister.org/news/learn2quote.htm l>
`for' is not different in ECMAScript and its
implementations from any other language of the C family.


Hmmm. In ECMAScript, the scope of variables declared in the initialisation
statement, is not the same as it is in C++.


ACK. I was referring to the test expression, this is a C-style `for'.
PointedEars
Jul 23 '05 #21
JRS: In article <c9************ *******@news.de mon.co.uk>, seen in
news:comp.lang. javascript, Mark Anderson <ma**@notmeyear dley.demon.co.u k
posted at Fri, 28 May 2004 16:45:50 : A 'for' loop takes 3 arguments (initialize; test; increment). The 'test'
must equate as true or false


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.getMon th()==4;J=new Date()) {}
for (J=new Date();J.getMon th()!=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()<12345678 90000) {}
do {} while (new Date()<12345678 90000)
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.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 23 '05 #22
"Mark Anderson" <ma**@notmeyear dley.demon.co.u k> writes:
Shame no books tutorials stoop so low as to explain this simple fact.
What about:
<URL:http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/stmt.html#10048 04>
<URL:http://msdn.microsoft. com/library/en-us/script56/html/js56jsstmfor.as p?frame=false>
I now know why I won't use ++ etc., except in the exceptional cases such
as Klaus just described.


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 - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 23 '05 #23
rh
Lasse Reichstein Nielsen wrote:
"Mark Anderson" <ma**@notmeyear dley.demon.co.u k> writes:
Shame no books tutorials stoop so low as to explain this simple fact.


What about:
<URL:http://devedge.netscap e.com/library/manuals/2000/javascript/1.5/reference/stmt.html#10048 04>
<URL:http://msdn.microsoft. com/library/en-us/script56/html/js56jsstmfor.as p?frame=false>


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
Jul 23 '05 #24

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2524
by: gi75research | last post by:
What should be a very simple function is going terribly wrong, and I don't know why. StartTime and EndTime are table values (formatted like "01:00A" or "02:00P"); DaypartStart and DaypartEnd are string values I specify when calling the function (initially tried passing time values & the same problem occurs); In the If statement, I can compare the exact same time values, and the varCurrentMinute >= varDaypartStart equality will not work;...
40
5730
by: Ike Naar | last post by:
In K&R "The C++ programming language (2nd ANSI C edition), the reference manual states (paragraphs 7.9 and 7.10) that pointer comparison is undefined for pointers that do not point to the same object. So if we have const char * foo = "foo" , * bar = "bar" ; int foobar = ( foo == bar ) ; would it mean that foobar is undefined?
14
2189
by: Clint Olsen | last post by:
I was wondering if it's considered undefined behavior to use a member of a union when it wasn't initialized with that member. Example: typedef unsigned long hval_t; hval_t hval_init(void) { union hval_init_u { double dbl; hval_t hval; }; union hval_init_u phi = { (sqrt(5) + 1) / 2 }; /* golden ratio */
19
78835
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not find one.
4
1675
by: Matt Burland | last post by:
I'm a little confused about the way the default equality operator works with classes. Here's the situation, I have two comboboxes that are each filled with different object (i.e. ComboBox1 contains objects of class A, ComboBox2 contains objects of class B). What I'm trying to do is determine if a given object is contained in one of the comboboxes, i.e.: Combobox1.Items.Contains(MyA); Combobox2.Items.Contains(MyB); Now the problem is...
21
3744
by: Geoff Cox | last post by:
Hello I have a TINYINT(1) field, group1, in a mysql data base and the value is 1 but php if ($row == "1") { does not work. What should I be writing here?
0
9591
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2816
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.