While loop

While loop is different from for loop, as no new variable is needed in this loop, and any current variable can be used to perform the tasks inside the while loop. An example is as follows:

while True:
perform instructions
if condition():
break

This is similar to for, but in this case the actions are performed first, and then the condition is checked. In the preceding example, the value of x is printed first, and we repeat the same set of instructions until the value of x reaches 10 or greater. Once the if condition is met, we break out of the loop. If we do not specify a break condition, we will go into an infinite loop with a increment of 1 for each x value.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset