mCurrentIndex -- How is it updated?



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

I know this line is to update the index as its function but I am not sure how this works in detail. Could anyone explain it?

Thanks a lot!



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

is used to make sure that value of ‘mCurrentIndex’ never exceeds the size of ‘mQuestionBank’.

I first tried to update it using ‘mCurrentIndex++’ (i know it is not at all a good option) and it didn’t work out fine for me as it exceeded the length of ‘mQuestionBank’ (as expected).


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

means that each time the updated value of 'mCurrentIndex ’ is divided by ‘mQuestionBank’ and then the remainder is taken so, when value of ‘mCurrentIndex’ is equal to value of ‘mQuestionBank’ then ‘mCurrentIndex’ get the value 0(zero). Therefore, when it comes to the end, the value ‘mCurrentIndex’ is set to zero again and thus, you see the first question on the screen and this keeps on in a cycle.

I hope that I was able to make you understand the concept.[ You can undersatnd it yourself, get a pencil-paper and do the dry-run and you’ll get it right:) ]

If you still not get it right you can ask me on the same forum.