473,761 Members | 5,758 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 #1
23 2209
In article <c9************ *******@news.de mon.co.uk>, Mark Anderson
says...
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);
}
This fails because after the first loop "y" becomes 12. Try
for (y=1; y!=5; y++)

Why do the first two fail? If i is value 5 then i==5 is true, as is
i===5.
The loop executes while the condition is true. In your example you
assign 1 to y, so the condition y==5 is false.

Can anyone explain what I'm missing here?


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
Jul 23 '05 #2
Mark Anderson wrote:

Hi Mark
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);
}
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. :-)
..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

Regards,
Erwin Moller
Jul 23 '05 #3
Lee
Mark Anderson said:

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?


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.

Jul 23 '05 #4

"Hywel" <hy**********@h otmail.com> wrote in message
news:MP******** *************** *@news.individu al.net...
In article <c9************ *******@news.de mon.co.uk>, Mark Anderson
says...
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);
}


This fails because after the first loop "y" becomes 12.


Why is the first iteration performed and how does 'y' become 12?

--
S.C.
Jul 23 '05 #5
Stephen Chalmers wrote:

Why is the first iteration performed and how does 'y' become 12?
x = 5;
for (y=1; y==5; y+=1) {
alert(x * y);
} S.C.

It doesn't.
The loop never starts, since y doesn't equal 5.

Mick

Jul 23 '05 #6
Lee

"Lee" <RE************ **@cox.net> wrote in message
news:c9******** *@drn.newsguy.c om...
Mark Anderson said:

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?

Without knowing you, it's hard to guess whether this is oversight
or if you really are missing something.
You're setting x=5 and y=1 and seem to be surprised that y!=5.


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
Jul 23 '05 #7
Mark Anderson wrote:
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.
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.
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.


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
Kl********@atme dia.net
http://www.atmedia.net/KlausRusch/
Jul 23 '05 #8
Mark Anderson wrote on 28 mei 2004 in comp.lang.javas cript:
This doesn't work...
x = 5;
for (y=1; (y==5); y+=1) {
alert(x * y);
}
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 !!!
..nor does...
x = 5;
for (y=1; (y===5); y+=1) {
alert(x * y);
}
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 !!!
...but this does..
x = 5;
for (y=1; (y<6); y+=1) {
alert(x * y);
}


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)
Jul 23 '05 #9
Klaus Johannes Rusch wrote on 28 mei 2004 in comp.lang.javas cript:
They are allowed, and there are cases where they make sense, for example

for (counter = 0; stillrunning == 1; counter++) {
if (something) stillrunning = 0;
}


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

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
5725
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
2186
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
78830
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
1673
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
3743
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
9554
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
9377
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
9925
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
8814
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
6640
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
5266
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
3913
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
3509
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2788
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.