Java: for loop, incompatible types

I am trying to run this for loop;

        for (int col= 0; grid[0].length; col++)

However, every time I try to compile, I get an error "incompatible types" - found int int, but expected logical value

I can’t understand what I am doing wrong!

+5
source share
4 answers

second operator: grid [0] .length is an integer. The second statement in the for loop is a condition statement and must be logical.

If you are trying to execute a loop while col is less than the length of the grid [0], you need this as a second statement:

col <grids [0] .length;

+10
source
for (int col= 0; col < grid[0].length; col++)   // See the typo
+2
source

grid[0].length - , . :

col < grid[0].length
+2

- :
for (int col= 0; col<grid[0].length; col++)

+1

All Articles