×

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

Python String startswith() method

Python String startswith() method The string.startswith() method in Python returns a boolean value ‘True’ if the given string starts with the prefix, otherwise it returns False. Syntax startswith(prefix[, start[, end]]) Parameter prefix: This parameter signifies the value to check. start(optional):...

1 minute read.

How to Configure Python Interpreter in Eclipse

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 lock

Before understanding the concept of Python-lock we need to understand what kind if condition we require to implement the locks in Python. Let us start with multithreading and its implementation...

5 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

Python Maurer Rose and Rhodonea Curves

In this tutorial, in Python we will make a Maurer Rose example and Rhodonea Curve example. Before we continue to see what precisely is maurer rose or a rhodonea bend...

10 minutes read.

Python Set issubset() method

Python Set issubset() method The set.issubset() method in Python returns True if all items in the set exists in the specified set, otherwise it returns False. Syntax set.issubset(set1) Parameter set- This parameter represents the set to check whether...

2 minutes read.

Idle python download for Windows

Here, we will be discussing how to get the answer to all questions related to installing Python on Windows. What is IDLE? Integrated Development and Learning Environment, or IDLE, is a shorthand....

3 minutes read.

Convert int to Float in Python using Pandas

Introduction We have already learnt about how to convert float variables to int. Now let us know how to convert int variables to float So let us create another Dataset on which...

2 minutes read.

Python type() Function

Python type() Function The type() function in Python returns the type of an object. The return value is a type object and generally the same object as returned by object.__class__. Syntax class type(object)      ...

1 minute read.

Python String rindex() method

Python String rindex() method The string. rindex() method in Python returns the highest index of the substring inside the string (if found). If the substring is not found, it raises an...

2 minutes read.

Python Tkinter Tutorial

What is Tkinter? Tkinter is GUI library of Python. When we integrated Tinkter with Python, it provides an easy and quick way to develop GUI applications. It provides the Tk GUI...

14 minutes read.

Count Number of Keys in Dictionary Python

Dictionary is a particular data type in python. Dictionary stores unique values by taking different keys and their assigned values. Through this article, we will learn about python dictionary count,...

3 minutes read.

Image to Text in python

Processing out information from an image is a very big task in all the fields of work such as in development and business sector. The process of converting an electronical...

3 minutes read.

How to Define a Function in Python?

What is a Function? In programming, a piece of code that executes a specific task or a group of related operations is known as a function. What does Python Functions Do? If you...

6 minutes read.

Convert XML to JSON in Python

XML conversion is very useful if we work on an API that returns data in JSON format and the source of data is in XML format. JSON A JSON file reserves the...

4 minutes read.

List Comprehension in Python

Sometimes we need new lists that are completely based on previously existing lists, so to perform this operation successfully, some of the programming languages come with a syntactic construct known...

5 minutes read.

Anaconda python 3 installation for windows 10

If you are a problem solver and like to solve programming questions, the anaconda distribution for python could be one of the best options to use and solve problems in...

3 minutes read.

Check whether dir is empty or not in python

Python: Check if a directory is empty In this tutorial, we will study how to check whether the director (dir) is empty or not in the python programming language. First of...

3 minutes read.

Pltpcolor in Python

Python: 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 is said...

3 minutes read.

How to convert Float to Int in Python?

A float value can be converted to an int via type conversion, an explicit method of transforming an operand to a certain type. But it must be noted that such...

3 minutes read.