while
loops, like the for loop, are used for repeating sections of code - but
unlike a for loop, the while loop will not run n times, but until a defined
condition is no longer met. If the condition is initially false, the loop body
will not be executed at all. In this blog, I’ll talk about how to achieve
while
loops about:
- syntax
- flow diagram
- basic using
- infinite loop
- nested while loop
- if-else statement with while loop
- loop of a list
Syntax
Here, statement(s)
may be a single statement or a block of statements. The
condition
may be any expression, and true is any non-zero value. The loop
iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately
following the loop.
Flow diagram
Key point of the while loop is that the loop might not ever run. When the condition is satisfied, the loop body will be executed; however, when the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
Basic using
We initially set count with value “0”. Then we continue to while
loop - when
count is smaller than 7, then print count value and assign “count” with its
value plus one; so when count is equal to 0, the script prints “The count is:
0”, count’s new value is 1 (0+1); and so forth until count’s value is 6.
The infinite loop
Here, the value of i
will always be smaller than 10, so it will print
“Infinite loop” without stopping.
Nested While Loops
Besides the basic using and the infinite loop, we can nest loops inside each other. This is also true for while loops where we can create multiple levels of while statements as long as proper indentation rules are followed.
The syntax is as follows:
Let’s look at the following example:
The initial values of x and y are -7 and 15, respectively. When x is smaller than or equal to y, print x’s value and assign its value with x plus one, then if x is also smaller than or equal to 0, print “X is negative” and reassign with x plus one, until x is greater than y.
If-else statement with while loop
You can include an else conditional statement
without the corresponding
if statement
in while loops as well.
The syntax is as follows:
Let’s look at the following example:
In this example, count is initially equal to 0. When count is smaller than 6, print current count’s value and reassign its value with count plus one, until count equals to 6, we print “The final count is: 6”
Loop of a list
List “names” contains 6 elements, i’s initial value is 0. Now we will start
while
loop. When i is smaller than names’ length (6), print names’ i-location
value and reassign i with i plus one, until i equals to 5, which corresponds
‘Michael’.
Conclusion
In this blog, we talked about syntax and basic using of while
loop, and its
supplementary applications: the infinite loop, nested while loop, if-else
statement with while loop and loop of a list. Hope it’s useful for you :)
Reference
- tutorialspoint, “Python while Loop Statements”, tutorialspoint, 2019. [Online]. Available: http://www.tutorialspoint.com/python/python_while_loop.htm
- python wiki, “While loops”, python wiki, 2017. [Online]. Available: https://wiki.python.org/moin/WhileLoop
- Dr. Andrew N. Harrington., “3.3. While Statements”, Hands-on Python Tutorial, 2017. [Online]. Available: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html
- trytoprogram, “Python while loop”, trytoprogram, 2017. [Online]. Available: http://www.trytoprogram.com/python-programming/python-while-loop
- KASIA MIKOLUK, “Python While Loop: A Guide to Using While Loops in Python”, udemy blog, 2013. [Online]. Available: https://blog.udemy.com/python-while-loop