The Python - While Loops
In this module, we will be discussing the while loop in python. As we have discussed before in this python tutorial that in this programming language there are basically two different types of primitive loop commands. And those two different types of primitive loop commands are mentioned below.
The While Loop
As a developer who is using this programming language if you wish to execute a set of different statements then to do that you can use the while loop. However, you should always remember that this loop works as long as the condition set for this loop is true. For example, if you wish to print i as long as i is less than six then
i = 1
while i < 6:
print ( i )
i += 1
In this above example if you forget to increment i then this loop will keep continuing forever so you should always remember to increment i in an example such as the one mentioned above. You should also remember that relevant values are required to be made ready in the while loop. Like in the example mentioned above we needed to define the indexing variable which was i. We set that particular indexing variable to 1 in the above-mentioned example. There are also many other python while loop exercises mentioned in this tutorial.
The Break Statement
If you wish to stop the entire loop even if the condition is true for the loop then you should use the break statement function. For example, if we want the loop to exit whenever i is 3 then
i = 1
while i < 6:
print ( i )
if i == 3:
break
i += 1
The Continue Statement
While using this programming language if you wish to stop using the current iteration and start with the next iteration then it is recommended that you should use the continue statement. If you wish to continue to the next iteration whenever i is 3 then the while loop python example for this is mentioned below
i = 0
while i < 6:
i += 1
if i == 3:
continue
print ( i )
With this, we finish our python while loop part of this entire python tutorial.