I can not understand how this code draws a complete chess board not only 8 squares
Especially : How for-loop it works.
import acm.program.*;
import acm.graphics.*;
public class chbord extends GraphicsProgram {
/* number of columns */
private static final int Ncolumns = 8;
/* number of rows*/
private static final int Nrows = 8;
public void run() {
int sqSize = getHeight() / Nrows;
for (int i = 0; i < Nrows; i++) {
for (int j = 0; j < Ncolumns; j++) {
int x = j * sqSize;
int y = i * sqSize;
GRect sq = new GRect(x, y, sqSize, sqSize);
sq.setFilled(((i + j) % 2) != 0);
add(sq);
}
}
}
}
The first time the outer loop runs, the inner loop runs completely (8 times). Then the outer loop runs a second time and the inner loop is then run completely once again (another 8 times).
This continues through the eighth row.
So you get 8 rows drawn, but each row is drawn as 8 columns.
Result: all 64 squares are drawn.
Especially : How for-loop it works.
A For-loop is a key part of programming. Here are some explanation articles: