The program change when swapping the condition in C++

Yesterday, when I did a coding problems, I met the errors in the code:

    while (count <= column * row) {
    if (matrix[rowOne + 1][columnOne] == count && rowOne + 1 >= 0 && rowOne + 1 < row) {
        count++;
        rowOne++;
    } else if (matrix[rowOne - 1][columnOne] == count && rowOne - 1 >= 0 && rowOne - 1 < row) {
        count++;
        rowOne--;
    } else if (matrix[rowOne][columnOne - 1] == count && columnOne - 1 >= 0 && columnOne - 1 < column) {
        count++;
        columnOne--;
    } else if (matrix[rowOne][columnOne + 1] == count && columnOne + 1 >= 0 && columnOne + 1 < column) {
        count++;
        columnOne++;
    } else {
        return false;
    }
}
return true;

I have already checked the code above this code, they were right but in this code, if I changed in each if and else if line, for example the line:

      if (matrix[rowOne + 1][columnOne] == count && rowOne + 1 >= 0 && rowOne + 1 < row)

I changed it to:

      if (rowOne + 1 >= 0 && rowOne + 1 < row && matrix[rowOne + 1][columnOne] == count)

The program was working but I didn't know why, can somebody help me?, Thank you!



Read more here: https://stackoverflow.com/questions/68489467/the-program-change-when-swapping-the-condition-in-c

Content Attribution

This content was originally published by Duy Trần Đình at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: