|
-----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 mailing list archive is Copyright 1997-2025 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.