0

When using java programming to display a spiral square, a if conditional statement is used to change the direction of the square array. But the order of the logic or condition in the if conditional statement is different.

public class SpiralMatrix {
    public static void main(String[] args) {
        int matrix[][] = new int[4][4];
        /*Spiral direction is clockwise, so x, y coordinate 
                 corresponding increase 1, unchanged or minus 1.*/
        int xd[] = {0,1,0,-1};   
        int yd[] = {1,0,-1,0};
        /*d=0,1,2,3 represents the right, down, left, and upper 
                  directions, respectively.*/
        int d = 0; 
        /*Next_x,next_y represents the subscript of the next matrix 
                  element, respectively*/
        int x=0,y=0,next_x,next_y;  
        for(int i=0;i<16;i++) {
            matrix[x][y] = i+1;
            next_x = x+xd[d];
            next_y = y+yd[d];

            /*When the subscript of the next matrix element does 
                          not exist or exceeds the boundary or the array 
                          element has been assigned a value*/
            if(next_x<0||next_y<0||next_y==4||next_x==4||matrix[next_x][next_y]!=0) {
                /*change direction*/
                d = (d+1)%4;   
                next_x = x+xd[d];
                next_y = y+yd[d];
            }

            x = next_x;
            y = next_y;

        }
        /*output spiral matrix*/
        for(int a=0;a<4;a++) {
            for(int b=0;b<4;b++) {
                System.out.print(matrix[a][b]+"\t");
            }
            System.out.println();
        }

    }

The above code outputs a correct result:

1   2   3   4   
12  13  14  5   
11  16  15  6   
10  9   8   7

But if you change the order of the conditions in parentheses in the if statement, for example:

if (matrix[next _ x][next _ y]! = 0 | | next _ x <0 | | next _ y <0 | | next _ y = = 4 | | next _ x = = 4)

Then run the program,the result shows that:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
    at code.SpiralMatrix.main(SpiralMatrix.java:19)
Criwran
  • 317
  • 3
  • 16
  • 1
    if next_y == 4 then you get the error since max index in the matrix is 3, remember that in a condition like this the program stops checking as soon as _one_ condition is true so the order matters. Compare to the quite common `if (object == null || object.someMethod())`, switching order will give you a null pointer exception – Joakim Danielson Sep 10 '19 at 08:58
  • Java uses [short-circuiting](https://en.wikipedia.org/wiki/Short-circuit_evaluation) for boolean expressions. – QBrute Sep 10 '19 at 08:59
  • Also see [Java logical operator short-circuiting](https://stackoverflow.com/a/8759917/1616627) – Roger Gustavsson Sep 10 '19 at 09:00

1 Answers1

1

In Java conditions are evaluated from left to right, if one of the OR conditions evaluates to true the remaining conditions to the right are not evaluated. In your original code:

if(next_x<0||next_y<0||next_y==4||next_x==4||matrix[next_x][next_y]!=0)

When next_y or next_x equals 4 matrix[next_x][next_y] is never evaluated since once of the previous conditions already evaluated to true.

If you put matrix[next_x][next_y] first you'll eventually get an ArrayIndexOutOfBoundsException since both of your array dimensions have a maximum index of 3.

Snagtz
  • 118
  • 6