views:

1890

answers:

7

Hello,

in the following example program in Java, I get infinite loop, and I cannot understand why:

public class Time {

    public static int next(int v) {
     return v++;
    }

    public static void main(String[] args) {

     int[] z = {3, 2, 1, 0};

     int i = 1;

     while(i < 4) {
      System.out.println(z[i]/z[i]);
      i = next(i);
     }
    }
}

In the while-loop the method next() is invoked, and i should be incremented every time by 1: next() should return i++, and the value of i in the while loop should be incremented by one.

Why could be the reason for the infinite loop? Thank you.

+13  A: 

You use post incrementation. That should work:

public static int next(int v) {
        return v+1; //or ++v
    }

so basically, your next function in your example, returns the same number, not incremented one.

empi
+3  A: 

Is there a reason you're not using the more idiomatic and easier to read:

for(int i = 0; i < 4; i++){
    System.out.println(z[i]/z[i]);
}

... or even:

for(int x : z){
    System.out.println(x / x);
}

... ?

Aaron Maenpaa
for(int x : z) {...} is not legal Java.
Darron
@Darron, yes it is - it's the enhanced for loop introduced in Java 5.
Dan Dyer
@My bad, I misread the types.
Darron
+1  A: 
public static int next(int v) {
        return ++v;
}

Don't forget arrays start with index zero in Java and division through zero throws an exception.

+8  A: 

The definition for v++ is:

Return the current value of v then add one to v

In your case, v is the method parameter which is a copy of the variable i in main, so the method just returns the same value that was passed to it. The incremented value in v is never used.

You could have used ++v. The definition of that is:

Add one to v, then return the new value
Adrian Pronk
For bonus points: what is the value of i after executing "i = i++;"?
Michael Myers
Off the top of my head, I believe the official answer is "undefined". But I'm not going to rush out and experiment.
Adrian Pronk
just tested and its unchanged (in actionscript).
Amarghosh
+1  A: 

You should use

public static int next(int v) {
        return v + 1;
}

Using v++ is wrong, as the returned value is the original value of v before incrementation.

Furthermore, using ++v or v++ in the return statement is bad style, because ++ has the side effect of modifying v, and there shouldn't be any side effects in expressions. Side effects should only be done in statements of their own to avoid confusion and undefined order of evaluation.

starblue
+5  A: 

The difference here is postfix vs prefix operators.

for example:

int a = 1;
int b = a++; // sets b to to 1 THEN increments a.

// a now equals 2.

int c = ++a; // increments a THEN stores the value in c, c = 3.

calling a method which returns int++ will not do what you expect. It is not the same as having this:

while (expression) {
// code...
intValue++;
}

because in the code block above, the increment is not being passed into a seperate variable, and the full incrementation of the value will occur prior to exiting the code block, postfix or prefix form.

public static int someMethod(int n) {
return n++;  
}

whereas the codeblock above will return n, then increment it later. But since your code will keep giving the same value to int n, it will keep giving the same value back since it is not incrementing your value, but rather the variable local to that method, n. Every time you pass a new value though, n is reset and will return the same thing, resulting in your infinite loop.

John T
A: 

post increment work that sure but u ll get exception bcas of using '0' value in array . logically 0/0 is not possible its infinite so exceptio occur avoid dividing by '0' value