You can print a checkerboard pattern by alternating stars and empty positions across a grid. Use nested for loops to visit each position and the remainder operator % to decide which positions contain stars.
Source code
Save this example as CheckerboardPatternExample.java.
public class CheckerboardPatternExample {
public static void main(String[] args) {
int size = 8;
for (int row = 0; row < size; row++) {
for (int column = 0; column < size; column++) {
if ((row + column) % 2 == 0) {
System.out.print("* ");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
Explanation
The variable size sets both the number of rows and the number of logical columns. The outer loop selects each row, and the inner loop visits each column in that row. Both counters start at zero, so a size of eight gives indexes from 0 through 7.
The expression (row + column) % 2 calculates the remainder after dividing the sum of the indexes by two. A remainder of zero means the sum is even; a remainder of one means it is odd. The program prints a star for an even sum and an empty position for an odd sum.
For the first two rows, the condition works like this:
| Row | Column | Sum | Remainder | Result |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | Star |
| 0 | 1 | 1 | 1 | Empty |
| 0 | 2 | 2 | 0 | Star |
| 1 | 0 | 1 | 1 | Empty |
| 1 | 1 | 2 | 0 | Star |
| 1 | 2 | 3 | 1 | Empty |
Moving one position horizontally increases column by one, changing the sum from even to odd or from odd to even. Moving one position vertically increases row by one and also changes the parity. This makes neighboring positions alternate in both directions.
Using only column % 2 would repeat the same arrangement on every row and produce vertical stripes. Including row shifts the arrangement on alternate rows, creating the checkerboard pattern.
Each star position prints "* ", and each empty position prints " ". Both strings occupy two character positions, so the columns stay aligned. System.out.println() runs after the inner loop to start the next row.
Run the example
With a JDK installed, open a terminal in the directory containing the source file and run:
javac CheckerboardPatternExample.java
java CheckerboardPatternExample
Output
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
* * * *
Display the output in a monospaced font to preserve alignment. Trailing spaces are omitted from the output shown here; leading spaces are preserved. Every row still contains eight logical positions, including empty positions at its edges. The stars mark alternating cells rather than drawing solid squares.
Try it yourself
Change size to 5. Odd sizes also work: the first and last positions of the first row both contain stars. With a size of five, the program prints 13 stars across 25 logical positions. With the original size of eight, it prints 32 stars across 64 logical positions.
Change the condition to (row + column) % 2 != 0 to swap the starred and empty positions. The top-left position will then be empty.
To make the empty cells visible while learning, replace System.out.print(" ") with System.out.print(". "). Both branches still print two characters, preserving alignment.
Finally, try size = 1, which prints a single star. A zero or negative size produces no output because the outer loop never executes.
