× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.


  • Subject: Re: Increment and decrement operators
  • From: "R. Bruce Hoffman, Jr." <rbruceh@xxxxxxxxxxxxx>
  • Date: Mon, 10 Jul 2000 16:17:30 -0400


-----Original Message-----
From: teresa garcia <teresa_a_garcia@hotmail.com>
To: JAVA400-L@midrange.com <JAVA400-L@midrange.com>
Date: Monday, July 10, 2000 3:54 PM
Subject: Increment and decrement operators


>Please, please, someone please help me understand why this two expressions
>are not equivalent:
>
>a[i++]++
>a[i++]=a[i++] + 1;
>
>The books I have only state they are not equivalent, I am suppose to guess
>the rest, but I don't find it trivial.


And it isn't, trivial that is, but...

in the second statement, the i++ is used and incremented twice.  On the RHS,
the expression is completely evaluated prior to assignment, so i is
incremented, _prior_ to the assignment.  I on the LHS is now one greater
than it was just prior to the evaluation of the RHS.

so... the first statement uses i to index and bumps that a[i].
and... the second uses i to index and bumps i and then uses that i again
(now bumped by one) to index the a[i] on the left.

clear?  maybe...

plug this code into a class and it's main...

    int [] a = new int[3];

    a[0] = 1;
    a[1] = 1;
    a[2] = 1;

    int i = 0;

    System.out.println("a[0] =" + a[0] + " a[1] =" + a[1] + " i =" + i);
    a[i++]++;
    System.out.println("a[0] =" + a[0] + " a[1] =" + a[1] + " i =" + i);
    a[i++]=a[i++] + 1;
    System.out.println("a[0] =" + a[0] + " a[1] =" + a[1] + " i =" + i);

then run it.  That may help..

ta.
===========================================================
R. Bruce Hoffman, Jr.
 -- IBM Certified AS/400 Professional System Administrator
 -- IBM Certified AS/400 Professional Network Administrator
 -- IBM Certified Specialist - AS/400 Administrator
 -- IBM Certified Specialist - RPG IV Developer

"Those who would give up essential Liberty,
  to purchase a little temporary Safety,
  deserve neither Liberty nor Safety."

     - The Papers of Ben Franklin


+---
| This is the JAVA/400 Mailing List!
| To submit a new message, send your mail to JAVA400-L@midrange.com.
| To subscribe to this list send email to JAVA400-L-SUB@midrange.com.
| To unsubscribe from this list send email to JAVA400-L-UNSUB@midrange.com.
| Questions should be directed to the list owner: joe@zappie.net
+---

As an Amazon Associate we earn from qualifying purchases.

This thread ...


Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.