mercredi 5 juillet 2017

else if & parentheses

why does these two code snippets have different outputs ?

The only difference between them is the parentheses around each if/else-if statement, but that shouldn't matter here, right?

while (1){
    if (i>=n&&j<0)
        break;

    else if (j<0)
        if (Arr[i])
            c++;

    else if (i>=n)
        if(Arr[j])
            c++;

    else if (Arr[i]==1&&Arr[j]==1)
        c+=2;

    i++;
    j--;
}

..

while (1){
    if (i>=n&&j<0){
        break;
    }
    else if (j<0){
        if (Arr[i])
            c++;
    }
    else if (i>=n){
        if(Arr[j])
            c++;
    }
    else if (Arr[i]==1&&Arr[j]==1){
        c+=2;
    }
    i++;
    j--;
} 

Aucun commentaire:

Enregistrer un commentaire