jeudi 14 juin 2018

breaking the else-if connection? (java)

i have the following code (its content doesn't matter much though):

    if (row+1>0 && row+1<mat.length) //valid move down
        if (nextVal==mat[row+1][clmn])
            return isPath (mat, val+1, row+1, clmn);

    if (row-1>0 && row-1<mat.length) //valid move up
        if (nextVal==mat[row-1][clmn])
            return isPath (mat, val+1, row-1, clmn);

    if (clmn+1>0 && clmn+1<mat.length) //valid move right
        if (nextVal==mat[row][clmn+1])
            return isPath (mat, val+1, row, clmn+1);

    if (clmn-1>0 && clmn-1<mat.length) //valid move left
        if (nextVal==mat[row][clmn-1])
            return isPath (mat, val+1, row, clmn-1);
 !!!!   else return false;
    else return false;

in order to be able to use else statement in regard to all ifs, i had to add the marked (!!!!) line. my question is: is there a way to remove that line and not have the else statement "lock" into the same block as the last if?

Aucun commentaire:

Enregistrer un commentaire