educative.io

Why does the highestSquareIdx decrement by 1 at the end of the while loop?

why does the highestSquareIdx decrement by 1 at the end of the while loop?


Type your question above this line.

Course: https://www.educative.io/collection/5668639101419520/5671464854355968
Lesson: https://www.educative.io/collection/page/5668639101419520/5671464854355968/4873047792156672

@Shamim_Imtiaz

The inner conditions comparing the left sides with the right sides and their increments and decrements are related to the square comparison. highestSquareIdx decrement by 1 in each iteration to traverse the whole loop. If we don’t decrement it, then we will be stuck where we started which will result in 0.