Why doesn't my code for making a chessboard in Processing work?

Viewed 239

I'm new to programming! I'm trying to make a chessboard using Processing (a language using similar syntax as Java).

Why is this not working?

void setup(){
  //5.b In setup() set the size to 350, 350 
  size(350,350);
  
  for (int y = 0; y < 8; y = y++){
    for (int x = 0; x < 8; x++){
      if ((y+x+1) % 2 == 0 ){
        fill(255);
      }else{
        fill(0);
      }
      rect(50*x,50*y,50,50);
      }
      
    }

}

```
1 Answers

The program is not working, because of this for loop, which runs forever:

for (int y = 0; y < 8; y = y++){

The reason is that the assigment y = y++ does the following:

  1. it evaluates the value of the right side, which will be y
  2. it processes the ++ operator, incrementing the value of y
  3. it processes the assigment, i.e. it assigns to y the value calculated in step 1, which is y

Therefore, the value of y never changes and the for loop never terminates.


Change it to this and it should work:
for (int y = 0; y < 8; y++){
Related