I just want to point out that:

if (i=w)
is not the same as:

if (i==w)

If you use a single equality sign then you assign a value to a variable.
If you use two equality signs then you...