Using while in Python

In this blog, I am going to explain some more about the use of while in your Python code. You can use while if the condition that you have needs to state as long as. The actual meaning of while is namely as long as. I am going to explain it with the easiest example.

The first you need is a variable with a value. In this example I see i=5. After this we can go use while if you want for example the value i to become bigger with 1 till it reaches 20. If you want to follow what happens with i, you can print i in the while loop. You can do that as follow by using a while loop:

It is also possible to put a break in you while loop. You achieve that by putting an if with the value it needs to break in your while. Like this (the value breaks at 9):

It is also possible to make a break of one value in you iteration. You do that with command continue. You need to shift i=i+1 to above the if and fore the rest, you only need to change break to continue. The iteration skips the value of i that you say after the if:

Hopefully you enjoyed this blog and learned some more about using while in your Python script.

Plaats een reactie

Ontwerp een vergelijkbare site met WordPress.com
Aan de slag