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

Output:

Recommended:  Nested Loop In Python Programming Language

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

Output:

Recommended:  Variables,Data Types and Type Conversion In Python Programming Language

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

Output:

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.

Recommended:  Comments, Indentation and Statement In Python Programming Language

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

Output:

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

Close Menu