How to use loop for this coding question

I dont understand how to use loop for this problem
as i am not able to define a condition to stop iterations