Python Looping Techniques
execution of a loop by using loop control statements like break and continue.
• The infinite loop
• Loop with condition at the top
• Loop with condition in the middle
• Loop with condition at the bottom
Python programming offers two kinds of loop, the for loop and the while loop. Using these loops along with loop control statements like break and continue, we can create various forms of loop.
The infinite loop
We can create an infinite loop using while statement. If the condition of while loop is always True, we get an infinite loop.
Example : Infinite loop using while
# An example of infinite loop
Write a python script to take input for a number and print its double?
Note: # press Ctrl + c to exit from the infinite loop
while True: num = int(input("Enter an integer: ")) print("The double of",num,"is",2 * num)
Enter an integer: 3
The double of 3 is 6
Enter an integer: 5
The double of 5 is 10
Enter an integer: 6
The double of 6 is 12
Enter an integer:
Loop with condition at the top
This is a normal while loop without break statements. The condition of the while loop is at the top and the loop terminates when this condition is False.
Example : Loop with condition at the top
i=1 while (i<=10): print(i) i=i+1