title: Kotlin for loops
body: <p>I'm trying to learn kotlin language (especially for android) but i found some difficulty to learn various types of for loop so what are the difference between normal for loop, for each loop and for each indexed and how to use it? </p>
  
  <p>For example in this article: <a href="https://medium.com/mindorks/building-a-customview-tictactoe-eb439f506505" rel="nofollow noreferrer">https://medium.com/mindorks/building-a-customview-tictactoe-eb439f506505</a> (that talks about android custom view), there is this part:</p>
  
  <pre><code>        squares.forEachIndexed {
          i, rects -&gt;
          for ((j, rect) in rects.withIndex()) {
              if (rect.contains(x.toInt(), y.toInt()))
                  return Pair(i, j)
              }
          }
  </code></pre>
  
  <p>where he use for each indexedto iterate through all the variables contained in squares (that is a 2d array) but i really don't understand how it works.
  Thanks to anyone who helps me!</p>