×

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

Scrimba python

Scrimba allows you to study whenever and wherever the topics or concepts you want. It also replaces classroom instruction with interactive screencasts, live events, and student-to-student help. Scrimba is an interactive...

3 minutes read.

Python max() function

Python max() function The max() function in Python returns the largest item in an iterable or the largest of two or more arguments. Syntax max(iterable, *[, key, default])               or max(arg1, arg2, *args[, key])   Parameter arg1, arg2, *args: This...

1 minute read.

Iterate through the list in Python

One of the six basic data types of the Python computer language is the list. You must be familiar with the methods and functions that deal with lists in order...

7 minutes read.

Decision Tree in Python

Decision Tree is one of the most essential algorithms in the area of machine learning for classification and regression. But let us first talk about the lifespan of every machine learning...

12 minutes read.

CSV Write in Python

What is meant by CSV? CSV stands for Comma Separated Values. The name itself defines its purpose. CSV arranges the data in the form of tables and stores the organized data in...

3 minutes read.

Sublime Python

SUBLIME: A compact, cross-platform code editor called Sublime Text 3 (ST3) is well-known for its quickness, usability, and robust community support. Although it's a fantastic editor out of the box, its...

6 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.

How to Convert int to str in Python

How to Convert int to str in Python In the last article, we studied how we convert a variable with string datatype to integer datatype and discussed different data types available...

4 minutes read.

What is Python online compiler?

The compiler is a program that is used to scan an entire high-level program (source code) and it translates the scanned program into machine code. Compilers convert .py source code into...

5 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

How to Concat two Dataframes in Python

Using Pandas dataframe, we can concat two dataframes or series in Python. So let's take a brief introduction to what is Pandas in Python. Pandas is a library typically used for...

7 minutes read.

GUI to Shut down, Restart and Logout from the PC using Tkinter in Python

GUI: One of the most significant factors that increased the usability of computer and digital technologies for common, less tech-savvy users is likely the development and widespread adoption of GUIs. GUIs...

4 minutes read.

Palindrome program in Python

Palindrome program in python A number or string is said to be a palindrome if we invert the number or string and the string or number remains the same as the...

3 minutes read.

Python pycountry

What is Pycountry? Python programming language: 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...

4 minutes read.

Python Variable Scope with Local & Non-local Examples

This article will examine Python's global, local and non-local variables and show you how to use them to write code without problems. Let's quickly review what a variable in Python is...

8 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Python Data Structures

Python Data Structures: Python is a programming language used worldwide for various fields such as building dynamic websites, artificial intelligence and many more. However, there is data that plays a...

18 minutes read.

Python Line Break

Introduction In this tutorial, you will learn line breaks in python.In Python, the new line character is used to indicate the start of a new line and the end of an...

5 minutes read.

Allocate a minimum number of pages in python

You have given a sorted array of size n which represents the number of pages in n different books and an integer value which denotes the number of students. We...

4 minutes read.

How to Install PIP In Python

How to Install PIP In Python The libraries for Python have made our work easier than we expected. From a simple addition of two numbers to applying algorithms on the big...

4 minutes read.