×

Python Control Statements

Control statements are under the roof topic of loops and loops in python are defined as iteratively and repeatedly working on source code. Loop control statements are defined as they are intended to change the path execution of the flow of program for its general sequence and the use of control statements is predominantly done here. At one point in the program, we get some requirements in which we want to end the loop completely and exit. And the situation may also arise such as iterating some of the loop statements before we enter the other loop. These loop statements sometimes also referred as Jump statements (or) Transfer Statements in Python.

Based on the flow of execution of the program the usage of control statements is performed. Generally, Python language supports the control statements which are of three types and they are:

  • Break
  • Continue
  • Pass

Break:

This break statement is used to break the execution of flow of the program based on the user requirements. It applies as it brings the flow outside the loop when the code is concentrated on another source block. And the break is only used in the code after if Condition inside the loop. If it is present in the nested loop the break will terminate the loop. In the whole code whenever the break keywords appear the interpreter comes out of the loop and executes the next immediate statements of the loop.

Example:

#Taking string as input
for i in ‘Dubai’:
   if i == ‘b’:
       break
print (‘current letter:’, Dubai)

Output:

>>> current letter: D
current letter: u

In this output window, we get that string as input INDIA and break statement works as the D in the string encounters with required conditional expression.

NOTE: When we use break in the inner loop, it mainly focuses on breaking out of only inner loop and the outer loop works and execution takes place significantly.

Continue:  

Continue statement works as in the code when continue appears the Python interpreter automatically jumps over the execution of the rest of the code statements in the respective loop and moves on with the next iterative statements. It is also used to end the particular iterative loop in which for or while is present and the continue keyword goes to the next iteration.

Example:

for char in ‘Dubai’:
  if (char == ‘b’)
      continue
print (“Current character:”, char)

Output:

Current character: D
Current character: u
Current character: a
Current character: i

In the above program the third iteration is executed as true so continue gets compiled and executes the statements respectively. Here, we can observe that for loop has been skipped and the remaining statements have been printed.

Continue doesn’t work on the principle of quitting out from the loop or not terminating rather it works on the principle of iterating the other next statements. Continue in Python works on the feature that it automates the control to the starting part of the loop. It is used to quit the iteration in which it is executing the loop and continue for the next iteration. Continue statement is not used to outside the loop because it depicts an error which is syntax error. Anyways continue is generally used in all types of loops but significantly used in for loop, while loop, and do-while loop. Continue is one of the reserved keywords in the Python.

Pass:

Pass is referred as null operation in Python. Pass is used in a situation where a user is not sure about the code and does not want to implement the code and there the pass statement comes into the picture and pass statement is implemented. Pass is used as an alternative for the empty space of the code and the user also avoids having error in it.

Example:

A = 5
for i in range(A) ;
#code that is to be deployed in future
for a in ‘CODE’:
if(i == ‘D’)
   pass print (i)

Output:

C
O
D
E

Note: Pass
“In this code after the for loop, there is nothing written but the user wants to execute the code so the user just uses pass keyword so that the interpreter skips that part of that code”.
The main role of pass is that it avoids errors that are compiled by an empty loop.


Related Topics

Return Statement In Python

Python Return Statement The return statement is generally used for the execution ending and returns the value back to the caller. The return statement can return all types of values and...

2 minutes read.

Find whether the given stringnumber is palindrome or not

Problem statement Sam is found of playing with strings. One day he thought of finding whether a string is a palindrome or not. He wanted to develop a computer process to...

2 minutes read.

Create a Table Using Tkinter in Python

Tkinter: The standard Python technique for building Graphical User Interfaces (GUIs) is Tkinter, which is included in all popular Python distributions. The only framework included in the Python standard library is...

3 minutes read.

Python program to find Fibonacci series

Python program to find Fibonacci series A Fibonacci series is an integer sequence of 0, 1, 1, 2, 3, 5, 8.... We can identify the Fibonacci series as any number sequence...

2 minutes read.

Python: SetBitmap() function in wxPython

SetBitmap() function In the last tutorial, we have discussed about the GetLabelText() function of wx.MenuItem class which is one of the important function of this class. Now, in this tutorial, we...

6 minutes read.

Python String count() method

Python String count() method The string.count() method returns the number of times a specified value appears in the string. Syntax string.count(sub[, start[, end]]) Parameter sub: This parameter represents a substring to be searched. start: This parameter...

1 minute read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

End Parameter in python

print(): The python print() function prints the program’s output to the output screen. The output can be an integer value, string value or other value. Syntax: print(“hi”) Output: hi will be displayed on the output...

3 minutes read.

Python program to check if two strings are anagram or not

Python program to check if two strings are anagram or not Problem: This is a python program that takes two strings and checks if given strings are anagram or not. Examples: Input: string1...

1 minute read.

Python bytearray()

Python bytearray() Class The bytearray() class is used to return a bytearray object which is an array of the specified bytes. It gives a mutable sequence of integers in the range 0...

1 minute read.

Isreal() Python

Python Programming Language Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Subprocess Module in Python

What is a Process? Every program will have its respective system state, i.e., the state in which it is running. A program that is in a running state or in a...

7 minutes read.

Python Subprocess Call Example

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

4 minutes read.

Python program to print array element present at even position

Python program to print array element present at even position In this program, we'll see a Python program that prints the elements of an array that are in even positions. We...

1 minute read.

Python math.cos and math.acos function

Math.cos() function In Python, the Math module is used for performing the mathematical operations. It includes the math.cos() function that is used for obtaining the cosine value of an angle in...

3 minutes read.

How to Convert Int to String in Python?

Every value we use or store in a variable in Python will have a specific data type. It describes the value's nature; based on that, Python will automatically assign a...

4 minutes read.

Python Functionalities of Pillow Module

This instructional exercise is about "Pillow" library, one of the significant libraries of python for picture control. Pillow library of python, is an easy to use and also open source...

14 minutes read.

Python Os sep

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Python Setup guide and installation

Python Installation guide Step by Step Python is widely used high-level programming language. The first version of Python was launched in 1991. Since then, Python has been gaining popularity. It is considered as...

4 minutes read.

Python Set remove() method

Python Set remove() method The set.remove() method in Python removes the specified element from the set if it is present in the set else this method will raise an error if the specified item does...

2 minutes read.