How to determine if prolog query will give an answer or not?
The order of clauses matters in Prolog: they are tried in order. So, the first clause is always tried first, and the second one would only be considered after the first clause fails. As you discovered, this never happens because the first gets stuck in a loop.

If Prolog used a breadth-first resolution algorithm, the clause order would not matter (but it would be far more consuming in terms of memory). Since instead SLD resolution is depth-first, the order does matter because of infinite paths which can lead to non-termination.