• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

++ operator

 
Sudhakar Jampa
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...");
}
}
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1646
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1071
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    if you are doing i++ there is no reason to assign it back to i.
     
    Ilja Preuss
    author
    Sheriff
    Posts: 14112
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    Sudhakar Jampa
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 1646
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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?
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic