Question

I'm trying to work through a project which creates a chessboard layout. The only part I can't get my head around is this line

if (row == 0)
    singleSquare.setBackground( i % 2 == 0 ? Color.black : Color.white );

else if (row != 0)
    singleSquare.setBackground( i % 2 == 0 ? Color.white : Color.black );

It seems to be 2 nested if statements but can anyone explain the logic behind it? It basically makes one square white then next square black etc for and 8,8 grid but I can't understand the logic. Thanks

Edit - Should have been more precise. What does this part mean?

i % 2 == 0 ? Color.black : Color.white

I will swap between 1 and 0 as the loop goes through I understand. What does the question mark do that makes it change from black to white?

Was it helpful?

Solution

It's a ternary statement combined with the modulus operator.

Ternary structure:

condition ? action-true : action-false

this results in your case to

i % 2 == 0 ? Color.black : Color.white 

Or written fully:

if(i % 2 == 0) {
    singleSquare.setBackground(Color.black);
} else {
    singleSquare.setBackground(Color.white);
}

Modulus:

i % 2 returns the remainder after divising i by 2. In your case this will result in an alternation between black and white (by adding 1 each iteration it will alternatively evaluate to true and false).

OTHER TIPS

i %2 == 0 is what is generally used to determine if an index is an even index.

In your example this means that the even indexes for row 0 will be set to black.

The other rows not equal to zero will also have every other spot being black or white. However they will be white for the even indexes.

Perhaps you are having trouble interpreting the ternary or ? operator in the statement below.

( i % 2 == 0 ? Color.black : Color.white )

Basically this is saying, if condition i%2 == 0 (index is even), carry out the first instructions after the question mark (set the color to black), else carry the instructions out after the colon (set the color to white).

if (row == 0)  
   singleSquare.setBackground( i % 2 == 0 ? Color.black : Color.white );

This means that if row == 0 (you probably have something like row = i % 2; above, making the rows alternate between 0 and 1), every other square will be black, and every other one will be white.

For row = 1 you have the same case, but starting with white.

You can read more about modulo (%) and Java's other operators here.

EDIT
Added the following after OP's clarifications:

singleSquare.setBackground(i % 2 == 0 ? Color.black : Color.white)

uses a ternary expression and is the same as

if (i % 2 == 0) {
    singleSquare.setBackground(Color.black);
} else {
    singleSquare.setBackground(Color.white);
}

% is the modulus operator. It produces the remainder of a division operation. So 3 % 2 == 5.

The ? is called a ternary operator. It can be used as a shorthand type of if/else statement. If the part to the left of the ternary operator evaluates to be true, then the value immediately following the ternary operator is used. If the part to the left of the ternary operator evaluates to be false, then the second value (following the : symbol) is used.

So in your first example, presuming i is an index of the chess board's column, the value supplied to setBackground() is Color.black when i is an even number (because i % 2 == 0) and is Color.white when i is an odd number (because i % 2 != 0).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top