+ 1

What does this code mean?

int mCurrentIndex = 0; mCurrentIndex = (mCurrentIndex+1) % mQuiz.length; Ps- mQuiz is in an instance of array object

21st May 2019, 5:27 AM
Ghost rider
1 Answer
+ 2
I suppose that : mQuiz.length is the number of questions in a Quiz Let it be 5 for example 0%5 = 0 hence index 0, so it returns the question on index 0. 1%5 = 1 hence index 1, so it returns the question on index 1. 2%5 = 2 3%5 = 3 4%5 = 4 5%5 = 0 ... As a result, you loop in quiz questions. Your code's equivalent is : if (mCurrentIndex + 1 < mQuiz.length) { mCurrentIndex++; } else { mCurrentIndex = 0; }
21st May 2019, 6:45 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar