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.
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;