Wrox Programmer Forums
|
BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1
This is the forum to discuss the Wrox book Beginning JavaScript, 3rd Edition by Paul Wilton, Jeremy McPeak; ISBN: 9780470051511
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 3rd Ed. ISBN: 978-0-470-05151-1 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 December 2nd, 2008, 09:29 PM
Registered User
 
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem with Increment Operators

Hi,

total newbie to javascript...

So I'm on Chapter 2, page 38 specifically. I am on the section that explains the use of ++ and --. I am trying to test out my understanding of the use of the Increment Operator. Here is the code I am trying:

<html>
<title></title>
<head></head>
<body

<script type="text/javascript">

var num;
var secnum;

num = 2;
secnum = num++;

alert(num);
alert(secnum);

</script>

</body>
</html>

I thought alert one would be 2, and alert two would be 3. But thats not the case. alert one is 3 and alert two is 2.

Can someone please explain why?


Thanks so much...

 
Old December 3rd, 2008, 01:18 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

There is a HUGE difference between
    x++ (known as POST-increment)
and
    ++x (known as PRE-increment)
and I'm more than a little surprised that the book you are using doesn't both explain it carefully and show specific examples.

I can't write a chapter in a book here, but let's see if we can make sense of this.

Let's start by explaining x++ vs. ++x.

It is true that AT THE END OF THE STATEMENT, both will have had the same effect on x. But WITHIN THE EXPRESSION that uses one or the other, the difference is clear:

++x means "first increment x and then get its value for use in the surrounding expression."

x++ means "first get the value of x and use that UNINCREMENTED value in the surrounding expression; THEN AND ONLY THEN increment x."

So in your example:
Code:
    num = 2;
    alert(num); // added for clarity
    secnum = num++;
    alert(num);
    alert(secnum);
Indeed num is initialized to a value of 2, as the first alert (as I added it) reveals.

So the next line is the secret.
    secnum = num++;

Now, recall what I wrote above: The increment of num takes place AFTER ITS NON-INCREMENTED VALUE is used in the expression.

In other words, it is just as if you had done:
    secnum = num;
    ++num; // as a separate step!

So you can clearly see that, BEFORE the increment, num has a value of 2 and THAT is what is copied into secnum. And then only AFTER that is num incremented to the value 3.

Okay??

**************************

Now, we have to be a *little* careful. I wrote that doing
    secnum = num++;
is the same as
    secnum = num;
    ++num;

But that's not strictly true. If you use the variable num *again* in the same expression, you will find that indeed it *HAS* been incremented.

It is instructive, perhaps, to try this code:
Code:
    num = 13;
    secnum = num++ + num;
    alert(num);
    alert(secnum);
Can you make sense of that???

In this statement:
    secnum = num++ + num;
what actually happens is
    13 [the existing value of num] is used as the left hand value for the + operator that is still to come.
    num is bumped up to 14
    14 [the now changed value of num] is used as the right hand value for the + operator.
    secnum gets the value 27.

So if you only use a post-incremented variable one time in a statement, then indeed you can treat it as if the increment happens after the statement is complete. It's only those rare occasions when you see the variable being used more than once that you have to be careful.

Okay?

So now test yourself:
    num = 101;
    secnum = ++num + num++;
what is the value of num? what is the value of secnum?
 
Old December 3rd, 2008, 11:51 AM
Registered User
 
Join Date: Dec 2008
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried it, and it makes sense to me.

Thanks O.P.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Logical operators ksskumar XSLT 3 November 11th, 2011 06:09 AM
Auto Increment Field Problem in SQL Server 2000 yes_no ASP.NET 1.0 and 1.1 Professional 4 August 11th, 2008 07:10 AM
Math operators nguyendh BOOK: Ivor Horton's Beginning Visual C++ 2005 1 June 25th, 2006 01:35 AM
Matrix operators Ravel99 Pro VB 6 0 April 28th, 2006 03:45 AM
Excel Increment Problem JimCognac Excel VBA 1 February 20th, 2004 11:52 AM





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