while <condition>:
<steps>
The condition is any expression that can be true or false (i.e. expressions that evaluate to a boolean). This is the same as you use on if
and elif
.
The way while loops work is:
Check the condition
a. If it is true: run the steps, go back to 1
b. If it is false: go to the first line after the loop (starting at the same column as while)
Note: If the condition is always true, your program will run forever! This is what is called an infinite loop.
Typically the loop condition will use a variable that is modified inside the loop, for example:
def mystery(n):
x = 0
total = 0
while x < n:
total = total + x
x = x + 1
return total
It turns out that some problems can be solved with a for loop or a while loop. We can convert the standard for loop into a while loop as shown below.
for i in range(n):
do_something()
i = 0 # initialization
while i < n: # termination condition
do_something()
i = i + 1 # increment
In fact, some people call for loops syntactic sugar since the functionality of a for loop can be achieved with a while loop
If while loops and for loops are very similar, which one is more powerful? It depends on the programming language. In some programming languages, for loops, and while loops are even closer, making them equivalent.
It is essential to master both and learn how and when to use each. Sometimes, a for loop can be more appropriate and human-readable. In other cases, a while loop is the best method.
The rule of thumb is the following. Use a while loop when you do not know how many times you need to repeat a sequence of steps, but you know you should repeat while some condition is True (or until a condition becomes False).
What's the output of the following function for n = 5?
def mystery(n):
x = 0
total = 0
while x < n:
total = total + x
x = x - 1
return total
There's no output. The function never terminates. This is called an infinite loop.
What's the output of the following function for n = 8?
def mystery(n):
i = 1
x = 0
while i < n:
i = i * 2
x = x + 1
return x
Even if the condition $i < n$ becomes False before the last increment of $x$, Python will complete the iteration before checking again the condition
What does this function do?
def mystery(n):
while(n < 100):
if n % 7 == 0:
return n
n = n + 1
return -1
Similar to for-loops, if the program flow hits a return in the middle of an iteration, it simply returns and does not run more iterations.
The code below, in particular, only runs the first iteration, and then stops.
Implement the function numberOfDigits(n)
that returns the number n
.
For example numberOfDigits(15110)
should return 5.
# this is one solution
def numberOfDigits(n):
d = 0
while n > 0:
n = n // 10;
d = d + 1
return d
# this is another solution
def numberOfDigits(n):
p = 0
while (n % (10 ** p)) != n:
p = p + 1
return p
print(numberOfDigits(123456789))
9
Implement the function countOneDigits(n)
that returns the number of digits of n equal to 1
. For example, countOneDigits(15110)
should return 3 (there are three ones in 15110).
def countOneDigits(n):
# solve it by yourself
return 42
assert( countOneDigits(15110) == 3)
assert( countOneDigits(42) == 0)
assert( countOneDigits(1010101) == 4)
assert( countOneDigits(0) == 0)