Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2006, 06:38 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default incrementing javascript loop use i++ or ++i

Code:
for (i=0; i<iNoOfOpts; i++)
{
    ///
    // code
    ////
}
Code:
for (i=0; i<iNoOfOpts; ++i)
{
    ///
    // code
    ////
}
when writing a js incrementing loop, should i use option 1 or 2, either, i++ or ++i?

Picco



www.crmpicco.co.uk
www.ie7.com
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
 
Old April 5th, 2006, 10:12 AM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 683
Thanks: 0
Thanked 1 Time in 1 Post
Default

Depends on what you are trying to do - ++i increments i before you use it, i++ increments i after you use it.

 
Old April 6th, 2006, 03:04 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

In other words.. if I were to write:

Code:
<script type='text/javascript'>

    $i = 0;
    alert('The value of $i is ' + (++$i)); //Outputs 1

    // Value of $i is now 1

    $i = 0;
    alert('The value of $i is ' + ($i++)); //Outputs 0

    // Value of $i is now 1

</script>
++$i increments the value before the value is inserted. $i++ increments the value after the value is inserted. You won't notice the difference between the two unless you're doing an increment operation as part of an expression.

Regards,
Rich

--
[http://www.smilingsouls.net]
Mail_IMAP: A PHP/C-Client/PEAR solution for webmail
Author: Beginning CSS: Cascading Style Sheets For Web Design
 
Old April 12th, 2006, 05:29 AM
Friend of Wrox
 
Join Date: Jan 2005
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default

Code:
for (var i = 1; i < 8; i++) 
{
still on the same subject, ive seen loops written like this above and below. are there any performance reasons here?

Code:
for (i = 1; i < 8; i++) 
{
thanks.

picco


www.crmpicco.co.uk
www.ie7.com
 
Old May 11th, 2006, 01:39 AM
Authorized User
 
Join Date: May 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kaos_frack
Default

Quote:
quote:when writing a js incrementing loop, should i use option 1 or 2, either, i++ or ++i?
Code:
for(var i=0; i<iNoOfOpts; ++i)
is the same as
Code:
for(var i=1; i<iNoOfOpts; i++)
Quote:
quote:are there any performance reasons here?
i don't know whether there are performance reasons
but there might be memory differences
cos in the first example ("var i=1") the variable 'i' is
declared as local and in the second it becomes global






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL Loop and incrementing a variable 2007-03-29 XSLT 8 March 30th, 2007 10:41 AM
javascript 'for' loop in xslt mummra1 XSLT 22 February 6th, 2007 04:26 PM
For Loop Not incrementing donrafeal7 Javascript 1 October 24th, 2006 12:24 AM
What am I doing wrong: Javascript For Loop interrupt Javascript How-To 4 June 19th, 2006 05:23 AM
get value from javascript and use in php loop ghalebro XML 1 February 23rd, 2005 07:53 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.