What happens in the previous algorithm in the following cases:

- If
*x*=*y*= 0?

The result is 0. - If
*x*= 0 and*y*> 0 (or vice-versa)?

The result should be*y*, but the algorithm enters an*infinite loop*. - If
*x*< 0 and*y*is arbitrary (or vice-versa)?

The algorithm enters an*infinite loop*.

Hence, if we want to take into account that the method could be called with arbitrary integer values for the parameters, it is necessary to insert a suitable test.

public static int greatestCommonDivisor(int x, int y) { if ((x > 0) && (y > 0)) { while (x != y) if (x > y) x = x - y; else // this means that y > x y = y - x; return x; } else System.out.println("wrong parameters"); }