Python3 Loops
The Loops condition allows to loop through the program code and repeat blocks of code until a given condition is fulfilled or not fulfilled. The code which is repeated is called the loop’s body.
There are two types of loops: the for loop and the while loop.
Loops have variables that change their values in every execution of the loop’s body and these variables have to be used as a condition either in the loop’s head or in the body to break the flow.
If you do not have a terminating condition in your loop you might end up with an infinite or endless loop that never terminates and you have to kill your application manually. Most of these loops are achieved while constructs. Let me show you examples:
while True:
print('I love Python!')
i = 0
while i < 1:
print('I love Python!')
|
This loop would eventually terminate if the worth of I might be greater than 1 at just one occasion. However, because the loop’s body does only printing to the console this may never happen.
Let’s check out the loop types intimately to ascertain how loops work and the way we will avoid endless loops.
Share this article with your friends
Post A Comment:
0 comments: