Recursive method not stopping even with condition

Viewed 62

recursion doesnt stop even when its reaches the conditon

 bool answer = trouverMot(tab, bankMots, x - 1, y, (longueurMatch + 1), motNumero, positions1) |
                      trouverMot(tab, bankMots, x + 1, y, (longueurMatch + 1), motNumero, positions1) |
                      trouverMot(tab, bankMots, x, y - 1, (longueurMatch + 1), motNumero, positions1) |
                      trouverMot(tab, bankMots, x, y + 1, (longueurMatch + 1), motNumero, positions1);
1 Answers

If I replace this code:

bool answer = trouverMot(tab, bankMots, x - 1, y, (longueurMatch + 1), motNumero, positions1) |
              trouverMot(tab, bankMots, x + 1, y, (longueurMatch + 1), motNumero, positions1) |
              trouverMot(tab, bankMots, x, y - 1, (longueurMatch + 1), motNumero, positions1) |
              trouverMot(tab, bankMots, x, y + 1, (longueurMatch + 1), motNumero, positions1);
return answer;

with:

bool answer = trouverMot(tab, bankMots, x - 1, y, (longueurMatch + 1), motNumero, positions1) ||
              trouverMot(tab, bankMots, x + 1, y, (longueurMatch + 1), motNumero, positions1) ||
              trouverMot(tab, bankMots, x, y - 1, (longueurMatch + 1), motNumero, positions1) ||
              trouverMot(tab, bankMots, x, y + 1, (longueurMatch + 1), motNumero, positions1);
return answer;

I get the expected output MANDOLINE.

I think you don't want to recursively search once you've already succeeded (the logical-or operator short-circuits evaluation, the bitwise-or operator does not).

Related