In each iteration, we update val and check if its value is less than 1000. The value of power tells us the maximum power n can have before it becomes greater than or equal to 1000. Think of it as a counter.
We can also use while loops with data structures, especially in cases where the length of data structure changes during iterations.
The following loop computes the sum of the first and the last digits of any integer: