Best post on control statements(break,continue,pass)-1

Control statements in python

Python consists of control statements to have control on functioning of loops.
This control statements are also called as terminating loops or skipping specific conditions.
The objects created during the normal execution are also destroyed.
Control statements includes break, continue and pass.

Control statements in python (itvoyagers.in)
<strong>Control statements in python</strong>

These statements are used to alter the normal flow of execution.

Break Statement (terminating loops)

The break statement is used to terminate the loop and to transfer the control of execution to the statement immediately following the loop.
If we are using nested loop than break statement will only exit the loop in which it is present. This means that break statement only exits single loop.
Break statement can be used to inject interruption externally by triggering some condition.
Both for loop and while loop can have break statement in it.
Break statement is executed when certain condition is satisfied as per the requirement of script.

break control statement in python(itvoyagers.in)
<strong>Break control statements in python</strong>

Syntax

break

1. Example of break control statement in for loop

""" Author : ITVoyagers (itvoyagers.in) 
Date  :25th August 2020 
Description : Program to show use of break control statement in for loop  """

for i in 'itvoyagers':
    if i=='y':
        break
    else:
        print("Current value of iterator is  ", i )

print("I am printed as break statement terminated the loop")

Output

Output of break control statement in for loop (itvoyagers.in)
<strong>Output of break control statement in for loop</strong>

2. Example of break control statement in while loop

""" Author : ITVoyagers (itvoyagers.in) 
Date  :25th August 2020 
Description : Program to show use of break control statement in while loop """

j=5
while j>0:
    print("current value of iterator is ",j)
    j-=1
    if j==2:
        break
print("I am printed as break statement terminated the loop")

Output

 

Output of break control statement in while loop(itvoyagers.in)

<strong>Output of break control statement in while loop</strong>




Continue Statement (skipping specific conditions)

The continue statement is used to skip the execution of the loop when a specific condition is encountered and transfers the control of execution to the start of the current loop and without executing other statement it again checks the condition before reiterating the loop.
If we are using nested loop than continue statement will only skip the specific condition present in single loop. .
Continue statement can be used to inject interruption externally by triggering some condition.
Both for and while loop can have continue statement in it.
Continue statement is executed when certain condition is satisfied as per the requirement of script.




continue-statement-itvoyagers
 

 

<strong>Continue control statements in python</strong>




Syntax

continue

1. Example of continue control statement in for loop

""" Author : ITVoyagers (itvoyagers.in) 
Date  :25th August 2020 
Description : Program to show use of continue control statement in for loop  """

for i in 'itvoyagers':
    if i=='y':
        continue
    else:
        print("Current value of iterator is  ", i )

print("I am printed after complete execution of script")

Output

 

Output of continue control statement in for loop(itvoyagers.in)

 

<strong>Output of continue control statement in for loop</strong>




2. Example of continue control statement in while loop

""" Author : ITVoyagers (itvoyagers.in) 
Date  :25th August 2020 
Description : Program to show use of continue control statement in while loop """

j=10
while j>0:
    j-=1
    if j==8 or j==3:
        continue
    print("current value of iterator is ",j)
    
print("I am printed after complete execution of script")

Output

 

Output of continue control statement in while loop(itvoyagers.in)

<strong>Output of continue control statement in while loop</strong>




Pass Statement

The pass statement is used when a statement is required syntactically but you do not want any command or script to be executed.
The pass statement is null operation having no effect on program execution.
Suppose if you want to create a function or method for future use and if you are still not defining its body then it is syntactically incorrect and to avoid such situation we can use pass statement.

Basically it will reserve that function name for future use and also function body can be defined later and script can be executed without any error.

Pass statement is similar to comment as both does not effect the flow of execution but only difference is that comments are generally ignored by interpreter but pass statement cannot be ignored.




Syntax

pass

1. Example of pass control statement in for loop

""" Author : ITVoyagers (itvoyagers.in) 
Date  :25th August 2020 
Description : Program to show use of pass control statement in for loop  """

for i in 'itvoyagers':
    if i=='y':
        pass
        print("-----------------Pass statement is used----------------------")
    print("Current value of iterator is  ", i )

print("I am printed after complete execution of script")

Output

 

Output of pass control statement in for loop

 

<strong>Output of pass control statement in for loop</strong>




2. Example of pass control statement in while loop

""" Author : ITVoyagers (itvoyagers.in) 
Date  :25th August 2020 
Description : Program to show use of pass control statement in while loop """

j=10
while j>0:
    j-=1
    if j==8 or j==3:
        pass
        print("-----------------Pass statement is used----------------------")
    print("current value of iterator is ",j)
        
print("I am printed after complete execution of script")

Output

 

Output of pass control statement in while loop

<strong>Output of pass control statement in while loop</strong>




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