Best post on infinite loop in python(looping) -Part 4

Infinite loop in python.

In programming there will be some statements or group of statements which we want to execute continuously for more than one time, this is where loops comes in the picture.
Looping allow us to continuously execute particular statement or block of statement to more than one time.
Looping will check for the condition, and will continuously execute particular statement or block of statement until the condition get false or invalid.
Repeated execution of a set of statements is called as iteration.
This statements are executed in order except when a jump statements are encountered.
Similar to other programming languages, python also supports infinite loop.
When the condition of any loop never becomes false, then the loop becomes the infinite loop.
The infinite loop means the result in the loop never ends.
We can use this type of loop in client server programming. In client server programming the server needs to run continuously so that client programs can communicate with the server whenever it is required.
In simple words infinite loop is the loop which will never terminates and it will run throughout the program, and execute the code.

Following are the types of loops in python programming.
1. for loop
2. for else loop
3. while loop
4. while-else loop
5. Nested loop
6. Infinite loop

Looping statements in python
<strong>Looping statements in python</strong>

Infinite while loop

In while loop there is a possibility that the condition never turns False .Such type of situations leads to the formation of infinite while loop.In infinite while loop statements are executed continuously as condition is always True.

Example of infinite while loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date :17th August 2020 
Description : Program for infinite while loop in python """

z=1
while z==1:
    x=input("Enter your name")
    print("hello",x)
print("I am infinite loop")

Output

Output of infinite while loop in python
<strong>Output of infinite while loop in python</strong>

Keyboard Interrupt

When we get stuck in to an infinite loop we can use keyboard interrupt to stop that loop.Infinite loop will affect memory, to stop it we have to generate interrupt
Keyboard interrupt is nothing but keyboard shortcut i.e. Ctrl+C.
Whenever we use shurtcut key an exception is raised which interrupts the normal execution of code.
This exception is nothing but KeyboardInterrupt

We can demonstrate the use of keyboard interrupt using above code.

Output of infinite while loop after using keyboard interrupt

keyboardinterrupt
<strong>Output of infinite while loop after using Keyboard Interrupt in python</strong>

Single Suite Statements for infinite loop

Just as we write if statement , we can also write while statement with its executable statement in while header if its executable block consist of single line.

Example of one line infinite while loop in python

""" Author : ITVoyagers (itvoyagers.in) 
Date :25th August 2020 
Description : Program to show use of one line infinite while loop in python """

indicator = 1
while (indicator): print ('Condition is True')

Output

single line infinite loop
<strong>Output of one line infinite while loop in python</strong>

Note: It is difficult to implement infinite for loop

For other python basics related posts:

Best post on programming language and its types – 1
Python Programming (Basics) – History & Features
Python Programming (Basics)- Debugging & Types of errors
Python Programming (Basics) – Variables, Values, Types & Keywords.
Best post on built-in functions in python – 1
Python Programming (Basics) – Type Conversion
Python Programming (Basics) – Comments,Indentation,Built-in Number Data types and Expressions
Best post on IDLE & script mode in Python – 1
Python Programming (Basics)- Operators in Python
Best post on Order of Operations – Python
Simple and compound statements of python in easy way
Best post on conditional statements in python – Part 1
Best post on conditional statements in python – Part 2
Best post on looping in python (for loop)-Part 1
Best post on looping in python (while loop)-Part 2
Best post on nested loop in python(looping) -Part 3
Best post on infinite loop in python(looping) -Part 4
Best post on control statements(break,continue,pass)-1
Best post on Function definition & calling in python -1
Easy flow of function execution,parameters,arguments-2
Easy fruitful & void function,return values,stack diagrams-3
Best post on types of function arguments in python- 4
Best post on recursive function, scope of variable-5
Best post on import and from statement in python – 1
Best post on modules and built-in modules math,random & time-2
Best post on user defined modules in python-3
Best post on string datatype in python – 1
Best post immutable string,string operations python-2
Best post on string methods in python – 3
Best post on list datatype in python – 1
Best post on mutable list, list operations python – 2
Best post on List methods in python – 3
Best post on dictionary datatype in python – 1
Best post on dictionary methods and operations-2
Best post on tuple datatype in python – 1
Best post on tuple operations and immutable tuple- 2
Best post on tuple methods and built-in functions-3
17 -Python program to demonstrate Button in tkinter and its event in easy way
New posts coming soon.......

For other advanced python related posts:

File Systems and File Handling in Python
Types of file modes and attributes of file object in Python
How to read,write and append in a file in python?
Remaining file methods in Python
File Positions in Python
Directory in Python and its methods
Iterator and Iterables in Python
Exceptions in Python
Exception Handling in Python (Part I)
Exception Handling in Python (Part II)
Regular Expressions
Metacharacters or Regular Expression Patterns
Functions in 're' module(Part I)- Match vs Search
Functions in 're' module(Part II)-findall(), split(), sub()
Flags for regular expressions(Modifiers)
GUI programming in Python and Python GUI Library
What is Tkinter ?
Layout Manager (Geometry Manager) in Python
Events and Bindings in Python along with Widget configuration and styling
Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
Best post:Dimensions, Anchors, Bitmaps & Cursors in Python
Canvas widget of tkinter module – Python
Widgets in tkinter module – Python
Label, Text, Entry & Message Widget in Python
Button, Checkbutton & Radiobutton Widget in Python
Best post on Menu and Menubutton Widget of tkinter
Best post- Listbox and Scrollbar(Slider) Widget-Python
Best post on Frame Widget of tkinter in Python
Best post: message box widget and its methods
Best post- LabelFrame, Toplevel, PanedWindow widgets
Best post on Spinbox and Scale Widget in Python
Best post : Database connectivity in Python (Part 1)
Best post : Database Connectivity in Python (Part 2)
Best post on Create and Insert query : Python + MySQL
Best post on Select Query to Search & Read: Python + MySQL-4
Best post on Update query and Delete query : Python + MySQL-4
New posts coming soon.......

Leave a Comment