Looping Techniques In Python Programming

Looping Techniques In Python Programming

In this article, you will learn about Looping techniques in Python programming. By this technique, we are able to control the execution of the loop by using statements like break and continue.

Two loops are offered by Python programming one is for loop and the second is while loop. With the help of these two loops along with break and continue statements we can use looping techniques in python programming to have multiple forms of the loops to control the workflow of loop execution.

In this article, you will learn the following things.

What is an Infinite Loop?
Example Of an Infinite Loop in Python Programming
Loop with a condition at the Top
Flowchart of the loop with a condition at the Top
Example of the loop with a condition at the Top
Loop with a condition in the Middle
Flowchart of the loop with a condition in Middle
Example of the loop with a condition at the Middle
Loop with a condition at the Bottom
Flowchart of the loop with a condition in Bottom
Example of the loop with a condition at the Bottom

What is an Infinite Loop?

A loop that never ends and keep continue his execution is called an infinite loop. A while loop can be used to create an infinite loop that never ends. But there is one must-have thing for the loop infinity and that is the condition of the loop must remain true all the time only then we get an infinite loop.

Example Of an Infinite Loop in Python Programming

# An example of infinite loop with while statement
# If you want to exit the loop then just press Ctrl + c
while True:
   number = int(input("Enter an integer number: "))
   print("The double of",number,"is",2 * number)

Output:

Enter an integer number: 5                                                                                                      
The double of 5 is 10                                                                                                           
Enter an integer number: 6                                                                                                      
The double of 6 is 12                                                                                                           
Enter an integer number: 4                                                                                                      
The double of 4 is 8                                                                                                            
Enter an integer number: 2                                                                                                      
The double of 2 is 4                                                                                                            
Enter an integer number: -3                                                                                                     
The double of -3 is -6

When the above loop executes it always shows you the double of any number you type on the screen no matter it is a positive number or negative number. It always shows a double of that number.

Loop with a condition at the Top

Like any other normal structure of a loop, this while loop is no exception. The first body of the loop starts then the test condition checks and when the test condition becomes false the loop terminates. This is all that a normal while loop does.

Flowchart of the loop with a condition at the Top

loop with a condition at the Top python programming
Loop with a condition at the Top in Python Programming

 

Example of the loop with a condition at the Top

# An example to illustrate a loop with a condition at the top

number = 20


number = int(input("Enter number: "))

#Below is the initialization of sum and counter
sum = 0
i = 1

while i <= number:
   sum = sum + i
   i = i+1    # update counter i evertime until the loop ends

# This will print the sum with added value
print("The sum is",sum)

Output:

Enter number: 20                                                                                                              
The sum is 210

Loop with a condition in the Middle

The second method of defining a loop is having a test condition of a loop in the middle of the loop body. It has an infinite loop execution along with a conditional break statement in between the body of the loop.

Flowchart of the loop with a condition in Middle

loop-with-a-condition-in-Middle-in-Python-Programming
loop with a condition in Middle in Python Programming

 

Example of the loop with a condition at the Middle

# Program to illustrate a loop with condition in the middle. 
# This will take input from the user untill a vowel is entered
vowels = "aeiouAEIOU"
# This is an infinite loop
while True:
   vw = input("Enter a vowel: ")
   # condition in the middle
   if vw in vowels:
       break
   print("That is not a vowel letter. Try again!")
print("Thank you and Happy Codding!")

Output:

Enter a vowel: w                                                                                                              
That is not a vowel letter. Try again!                                                                                        
Enter a vowel: t                                                                                                              
That is not a vowel letter. Try again!                                                                                        
Enter a vowel: e                                                                                                              
Thank you and Happy Codding!

In the above example, we set a condition in the middle of the loop with the help of a break statement. The condition is there is a prompt message for the user to type any letter and until the letter is not vowel the loop continues its execution.

Loop with a condition at the Bottom

This kind of loop pretty much the same as do while loop in which the body of the loop must execute at least once and then the condition is checked. In the same fashion, we can implement this infinite loop by using a break statement at the end.

Flowchart of the loop with a condition in Bottom

loop-with-a-condition-in-Bottom
loop with a condition in Bottom

Example of the loop with a condition at the Bottom

# Python program to illustrate a loop with the condition at the bottom
# Roll a dice untill user chooses n
# import random module from Python library
import random
while True:
   input("Press Enter To Roll The Dice ")
   # you should get a number between 1 to 6
   number = random.randint(1,6)
   print("You got the number",number)
   selection = input("Roll again?(y/n) ")
   # condition
   if selection == 'n':
       break

Output:

Press Enter To Roll The Dice                                                                                                    
You got the number 3                                                                                                            
Roll again?(y/n)                                                                                                                
Press Enter To Roll The Dice                                                                                                    
You got the number 1                                                                                                            
Roll again?(y/n)                                                                                                                
Press Enter To Roll The Dice                                                                                                    
You got the number 4                                                                                                            
Roll again?(y/n) n

In the above example, we set the condition at the bottom of this infinite loop. Each time the user hits the enter the dice rolls out and displays a number between 1 to 6. A random number continues to display until the user selects the letter ‘ n ‘ for termination of the loop execution.

Jack Sparrow

Hay I Am Admin Of This Website. I Am Very Passionate About Computers And Technology. This Website Is The Result Of My Passion. I Love To Eliminate The Barriers That Come In Seeking Education. This Website Is The Free Source Of Learning About New Technology And Up To Date Knowledge.

Leave a Reply