Percent symbol on page 42

On page 42 of the 3rd edition of Android programming book there is a percent sign used in the below line of code. Does anyone know what this percent sign does in this line. I am unable to find online. Thanks in advance.

mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;

That’s a modulus operator: http://www.cafeaulait.org/course/week2/15.html

Short definition of it in my words, it’s division, but your answer is the remainder

Example: 10 % 3 = 1 , because 3 goes into 10 3 times, leaving 1 left over.

Hope this helps!

Hi, I understand it as a way to “update and restart” the index value (mCurrentIndex) so for example if the current mCurrentIndex value is, lets say 1 and the Bank.length is 10 you would have:

mCurrentIndex = (1+1)%10, so it “updates” the index to 2 (i.e. the next question in the array),
the same goesfor every index until you reach the last question.

In this case you would have a remainder % of 0, and the index would “restar” all over again, showing you once again the first question in your question array.

Can anyone tell me if I´m right or wrong? Thanks to all.

Yes, You’r right, and if you want to do it backward you may use :

myCurrentIndex= ( myCurrentIndex - 1 + mQuestionsesBank.length)%mQuestionsesBank.length;