3

Hello can someone please explain why this would make an infinite loop. Thanks!

    public static void main(String[] args) {
        int x = 5;
        int y = 10;
        
        while (x < y) {
            x = getX(x);
        }

    }// end main
    
    public static int getX (int x) {
        
        x = x++;
        
        return x;
    }

6 Answers6

5

x++ is an expression. Expressions are 'resolved' (calculated); in other words, they resolve into a value. For example, the expression 5 + 2, if resolved, resolves as value 7.

x++ is a bit of a weird expression; it's one that has side effects. It doesn't just calculate, the act of resolving it, all by itself, causes things to occur. Thus, it does 2 things: It resolves into a value, and it causes other stuff to occur as well.

Specifically, x++ means: Return the value of x as it is right now. However, as a side effect, increment x (but, the value of this expression is x as it was before you increment it).

x = someExpr is java-ese for: Resolve expression someExpr, then set x to have the calculated value.

Thus, x = x++ is silly code that you never want to write. Let's say x is 5 when we start this, then:

  • First, we 'resolve' x++, meaning, the value is going to be 5.
  • But before we're actually done, increment x. x now holds the value of 6, but the expression's value is 5.
  • Now overwrite whatever x was holding with the value of what the expression is, which is 5.

Thus, x is... still 5.

If you want to increment x and that's all, just :

public static int getX(int x) {
  x++;
  return x;
}

or even simpler:

public static int getX(int x) {
  return x + 1;
}

is what you want.

rzwitserloot
  • 65,603
  • 5
  • 38
  • 52
1

Change x=x++; as x=++x; because now you are not changing x value.

fırat dede
  • 146
  • 7
1

Remove the assignement to x

You can simply do :

public static int getX (int x) {
    x++;
    return x;
}
Audran
  • 11
  • 1
1

the code youve written is very long you can write it shorter. like this. and this has to work too:

 public static void main(String[] args) {
    int x;
    int y = 10;
    
    for (x = 5; x < y; x++) {
        //do your things
    }

}// end main
ApeSander
  • 21
  • 2
1

getX doesn't actually change the value of x. x++ increments the variable then returns the ORIGINAL value before the increment. So let's say we call x = getX(x) when the value of x is 5. What happens?

Pass value of x variable in main function (which is 5) to x parameter in getX function. Now we're in getX function. Increment the value of x by 1. Now the value of x is 6. Assign the original value of x back to x. This assigns 5 back to x. Return the value of x from getX function, which is 5. Assign return value of getX function to x variable in main function. The value of x in main function is still 5, and it will stay that way.

You can replace x=x++ with any of the following:

Compute the value x + 1 and assign back to x.

x = x + 1

Increment x and return the value AFTER the increment to x. (the assignment back to x is actually redundant)

x = ++x

Add the value 1 to the x variable.

x += 1

Increment the x variable by 1.

x++

I recommend the 3rd or 4th option. If you have any other questions about the prefix and postfix ++ operators feel free to ask. Hope this helps.

1

x++ is a kind of Post-increment operator:

A post-increment operator is used to increment the value of the variable after executing the expression completely in which post-increment is used. In the Post-Increment, value is first used in an expression and then incremented.

So your example

public static int getX (int x) {  
    x = x++;
    return x;
}

is equivalent to

public static int getX (int x) {  
    x = x;
    x + 1;   // but this result doesn't assign to x
    return x;  
}

As a result, your x = getX(x); is always 5. That is why you got an infinite loop.

Kai-Sheng Yang
  • 1,371
  • 2
  • 12
  • 19