This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes ++ operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "++ operator" Watch "++ operator" New topic
Author

++ operator

Sudhakar Jampa
Greenhorn

Joined: Jan 29, 2005
Posts: 7
what's the wrong with the following code. it is running in to infinite loop.
as per my understanding at some point 'i' value should cross 10.

public static void main(String args[]) {
for(int i=0;i<=10;i=i++){
System.out.println("something wrong...");
}
}


SCJP 1.5, PMP
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The increment and decrement operate on the variable itself. You do not need to assign the result to the same variable; in fact, you usually don't. If anything, you use the result in some other calculation or assign it to a different variable.

Since you are using the post-increment operator (++ is after i), here is the sequence of operations the first time through the loop.
  • The current value of i (0) is pushed onto the stack.
  • i is incremented by 1 (i is now 1).
  • The value on the stack is assigned to i (i is now 0).

  • This will continue indefinitely since i comes out of the "increment step" of the for loop with the same value (0) which will clearly never be >= 10.

    Another way to say that (in English) is that the subexpression "i++" evaluates to the current value of i, before it is incremented. That i's value is changed as well is called a "side effect" of the expression. Note that a simple expression like "3 + 4" or "i * 2 - 8" has no side effects; it merely evaluates to (returns) a value.

    The increment operators are handy for other constructs, too, like setting a few array elements:andThere is also a pre-increment (and decrement) form: put the ++ (or --) before the variable:
    [ January 29, 2005: Message edited by: David Harkness ]
    Sudhakar Jampa
    Greenhorn

    Joined: Jan 29, 2005
    Posts: 7
    thanks for your response david.

    but if you see the operation i=i++; what I am thinking is

    say i = 0 first time.

    step 1 : assing value to i (i=i; this is once again existing i value, so it is 0).
    step 2 : increment i value by 1 (that is existing i value + 1, so 0 + 1).

    that's what the meaning rght?
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    The trick is that the "post-increment" happens immediately after the current value of i is pushed onto the stack but before it is assigned to i. The increment does not wait until the end of the entire statement. That's why the order of operations is as I listed above.

    The whole point of post-inc/decrement is that you get to use the old value of the variable in the rest of the expression.

    Note thatworks the way you wanted: increment i then assign new value of i to i.

    While you should never, ever do this in real life, note that is has appeared on a mock SCJP certification exam.

    For more details, see
  • A JavaRanch post
  • Another JavaRanch post
  • This Corey's Tipline entry
  • If you really want more after all that, grab yourself a copy of the Java Language Specification.
    [ January 29, 2005: Message edited by: David Harkness ]
    Steven Bell
    Ranch Hand

    Joined: Dec 29, 2004
    Posts: 1071
    if you are doing i++ there is no reason to assign it back to i.
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Hi "jampa",

    please adjust your display name to our naming policy - most specifically, use a first and a last name.

    Thank you very much, and welcome at the Ranch!


    The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
    Sudhakar Jampa
    Greenhorn

    Joined: Jan 29, 2005
    Posts: 7
    David Harkness, Thanks for the clarification. I will go through the java specs also.

    Ilja Preuss, I corrected my display name.
    David Harkness
    Ranch Hand

    Joined: Aug 07, 2003
    Posts: 1646
    Glad to help, Sudhakar, and welcome to JavaRanch!

    By the way, is it offensive to uppercase someone's name when they don't? I just assume people don't do it themselves because they are new typists, but it just occurred to me that some cultures might think it's rude. Anyone know?
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: ++ operator
     
    Similar Threads
    doubt regarding garbage collection
    Errata on Simulators?
    Are facts open to interpretation?
    Array
    Question from Marcus Green