xxxxxxxxxx
var count = 1
while (count <= 10) {
println(count)
count += 1
}
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:
xxxxxxxxxx
n = 2 # Could be any number
power = 0
val = n
while val < 1000:
power += 1
val *= n
print(power)