-3

I am learning kotlin and in a snipped of code iterating through a list i have this line: currentIndex = (currentIndex + 1) % myList.size

I am confused by the % sign. Does it mean that you should increment currentIndex by 1 until you get to the size of myList?

I couldnt find anything in documentation about it and the text book just seems to assume I should know!

  • That is a common operator in many languages. While the semantics may differ, it's commonly used for as a "modulus" or "division remainder" operator. Any decent book or tutorial or class should have taught you that. – Some programmer dude Dec 08 '20 at 19:55
  • In C, the meaning of modulo is slightly ambiguous. In Java and Kotlin, it's just the remainder operator, so there's no ambiguity. – Tenfour04 Dec 08 '20 at 21:17

1 Answers1

1

Modulus operator. Take a look to the next links:

EDIT 1: Sorry, I forgot to answer your question really.

What it makes that statement is to increment the currentIndex by 1 at each iteration, until it ranges the whole list. When that happens, the currentIndex is initialized to 0 again. When currentIndex = 9, the next iteration will be set to zero again.

Cheers.

Arritmic
  • 474
  • 2
  • 12