473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

why does counter not stop?

bob
Hi,

Question about For (...).
I did this:
for (i=1;i=5;i++)
alert(i)

and i get an infinite loop with value '5'. I thought that 'i=5' was the test
which must be 'true' in order to execute 'alert(i)'. It's normal that i
don't get 1 to 4 because in those case, the test is false. When i=5, i get
alert(i) with 5 but appearantely, i never equals 6.

By the way, is there a difference between i++ and ++i? I tried both but
didn't find any difference.

Thanks
bob
Jul 20 '05 #1
2 1567
bob wrote:
Question about For (...).
I did this:
for (i=1;i=5;i++)
alert(i)

and i get an infinite loop with value '5'. I thought that 'i=5' was the
test which must be 'true' in order to execute 'alert(i)'.
Two problems:

(1) The second parameter of the for loop has to be true to keep looping. It
will execute the content of block every time it loops.

(2) "=" is the assignment operator. The numerical test operator is "==".
It's normal that i don't get 1 to 4 because in those case
Nope. The first time round the loop you set i to 1, and then immediately
afterwards you set it to 5.
, the test is false.
i=5 is always true
When i=5, i get alert(i) with 5 but appearantely, i never equals 6.
Becuase you add one to it, and then set it back to 5 again.
By the way, is there a difference between i++ and ++i? I tried both but
didn't find any difference.


It's a case of when the increment happens.

<script type="text/javascript">
i = 0;
document.write (i +'<br>'+ i++ +'<br>'+ i +'<br>'+ ++i +'<br>'+ i);
</script>

might make things clear if you follow it through carefully.

--
David Dorward http://dorward.me.uk/
Jul 20 '05 #2
bob
thanks for replying
"David Dorward" <do*****@yahoo.com> wrote in message
news:bh******************@news.demon.co.uk...
bob wrote:
Question about For (...).
I did this:
for (i=1;i=5;i++)
alert(i)

and i get an infinite loop with value '5'. I thought that 'i=5' was the
test which must be 'true' in order to execute 'alert(i)'.
Two problems:

(1) The second parameter of the for loop has to be true to keep looping.

It will execute the content of block every time it loops.

(2) "=" is the assignment operator. The numerical test operator is "==".
It's normal that i don't get 1 to 4 because in those case


Nope. The first time round the loop you set i to 1, and then immediately
afterwards you set it to 5.
, the test is false.


i=5 is always true
When i=5, i get alert(i) with 5 but appearantely, i never equals 6.


Becuase you add one to it, and then set it back to 5 again.
By the way, is there a difference between i++ and ++i? I tried both but
didn't find any difference.


It's a case of when the increment happens.

<script type="text/javascript">
i = 0;
document.write (i +'<br>'+ i++ +'<br>'+ i +'<br>'+ ++i +'<br>'+ i);
</script>

might make things clear if you follow it through carefully.

--
David Dorward http://dorward.me.uk/

Jul 20 '05 #3

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

Similar topics

54
by: seberino | last post by:
Many people I know ask why Python does slicing the way it does..... Can anyone /please/ give me a good defense/justification??? I'm referring to why mystring gives me elements 0, 1, 2 and 3...
10
by: anonymousnerd | last post by:
Apologies if this question seems stupid: How does one write a function that increments a value in Python? When I tried, the variable never changed. The session went like this: >>> def...
5
by: bughunter | last post by:
Hi, Consider this code: ---- Monitor.Pulse(oLock); Monitor.Exit(oLock); ---- If a thread was waiting on oLock then will the current thread
7
by: Delphi Sleuth | last post by:
what the heck am I not getting. This little snipet doesn't do what I thought it was going to do. Can anyone tell me why? I expected 2 Hello: 102 All I get is the 2. I had been trying to...
14
by: Johan Christensson | last post by:
Hi. I got contacted by an old teacher today that works at a public shool here in Sweden. They have a ever groving problem with the students playing Counter Strike on the schools computers. Not...
1
by: Drew | last post by:
I would like to find out how to build a click counter using VB.NET. This is not a run-of-the-mill click counter (i.e. for web use), but a novelty app that counts how many times you click...
2
by: himanshubahl | last post by:
Hi, I have created a small map like this. typedef map<int,string> TEST_MAP; TEST_MAP testMap; I add about 10,000 string entries into the map like this tempStr="test Program"; for(int...
9
by: =?utf-8?B?5Lq66KiA6JC95pel5piv5aSp5rav77yM5pyb5p6B | last post by:
I define the class like this: class AAA: counter = 0 def __init__(self): pass def counter_increase(): AAA.counter += 1 print "couter now :", AAA.counter But how could I call the function...
2
by: majorecono | last post by:
I took a look at the site and found this as a counter. Is there any way I can use it in a dialog box, that asks user to input any number of letters in a (?)dialog box, pressing enter and out comes...
13
by: Adhal | last post by:
Hi, How can I stop hotlinking to a specific file, and I want it to redirect it to a PHP link so I can monitor the number of downloads. Here is my site with the download page:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.