Table of contents
1.
Introduction 
2.
Break Statement in Python
2.1.
Execution Flow for Break Statement
2.2.
Flow Chart For Break Statement
2.3.
Examples of Break Statements
2.3.1.
Implementation in for loop
2.3.2.
Explanation
2.3.3.
Implementation in while loop
2.3.4.
Explanation
2.4.
Advantages Of Using Break Statements
3.
Continue Statement in Python
3.1.
Execution Flow for Python Continue Statement
3.2.
Flow Chart For Python Continue Statement
3.3.
Examples of Python Continue Statement 
3.3.1.
Implementation in for loop
3.3.1.1.
Output
3.3.1.2.
Explanation
3.3.2.
Implementation in while loop
3.3.2.1.
Output
3.3.2.2.
Explanation
3.4.
Advantages Of Using Continue Statements
4.
Pass Statement in Python
4.1.
Execution Flow for Pass Statement
4.2.
Implementation of Pass Statement
4.2.1.
Output
4.3.
Advantages Of Using Pass Statements
5.
Difference Between Continue and Pass in Python
6.
Some Common Mistakes To Avoid While Using Break Continue and Pass In Python
7.
Frequently Asked Questions
7.1.
What is break continue and pass in Python?
7.2.
Why do Python developers use continue break and pass?
7.3.
What type is pass in Python?
7.4.
What is the difference between break continue and pass statements?
7.5.
What is continue used for in Python?
7.6.
How do you skip or continue in Python?
7.7.
Should you use Python continue?
8.
Conclusion
Last Updated: Sep 30, 2024
Medium

Break, Continue, Pass in Python

Author Nitika
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction 

Break Continue Pass in Python are some of the most fundamental topics in your journey to learning Python programming. These mighty statements are called the Loop Control Statements and are here to help you in loops.

In this article, we will know the Break Continue Pass statement implementations and examples.

Break Continue Pass in Python

Break Statement in Python

break

The break statement in Python lets you terminate the current loop and resume the program at the following statement. 

In a nested loop, the break statement terminates the loops containing the break statement.

The most common use of break statements is in for and while loops when an external condition requires an exit from the loops.  

Execution Flow for Break Statement

for loop
    #code block
    statement 1
    statement 2
    .
    .
    .
    if condition:
        break
    statement 3
    statement 4
    .
    .
    .
    #remaining code block
You can also try this code with Online Python Compiler
Run Code

Flow Chart For Break Statement

Flow Chart For Break Statement

Examples of Break Statements

Now that we have understood the break statement, we move forward to implementing it.

Implementation in for loop

list1 = ['Coding', 'Ninja', 'Courses', 'are', 'Java', 'Cpp', 'Python', 'Full Stack', 'Data Science'] 
j = 0

for j in range(len(list1)):
    print(list1[j])

    if (list1[j] == 'Python'):
        print('\nPython \n')
        break
        print('\nAfter break statement\n')
    j += 1


print('\n \nThis is an example of break Statement in for loop')
You can also try this code with Online Python Compiler
Run Code

 

Output

Coding
Ninja
Courses
are
Java
Cpp
Python


Python 



 
This is an example of break Statement in for loop
You can also try this code with Online Python Compiler
Run Code

 

Explanation

In the above program, a list with a number of elements is created. The for loop iterates over the entire list1 in search of the word 'Python' in the sequence. The if statement is used for checking in the string is equal to Python. The loop is exited using the break statement if the strings are equal.

Implementation in while loop

list2 = ['Coding', 'Ninja', 'Courses', 'are', 'Java', 'Cpp', 'Python', 'Full Stack', 'Data Science'] 
j = 0

while True:
    print(list2[j])
    if (list2[j] == 'Python'):
        print('\nPython \n')
        break
        print('\nAfter break statement\n')
    j += 1


print('\n \nThis is an example of break Statement in while loop')
You can also try this code with Online Python Compiler
Run Code

 

Output

Coding
Ninja
Courses
are
Java
Cpp
Python


Python 



 
This is an example of break Statement in while loop
You can also try this code with Online Python Compiler
Run Code

 

Explanation

In the above, the program searches for the word 'Python' from the list. 

In the while loop, each component from the list is compared to 'Python' by the if statement. The break statement is executed and the loop is terminated when the if-condition is true.

Advantages Of Using Break Statements

  • Using a break statement is the most evident and easiest way to exit out of intensely nested loops. 
     
  • It increases the code readability as it is used to avoid any complex nested loops.
     
  • it increases the control over the program flow. 

Continue Statement in Python

continue
You can also try this code with Online Python Compiler
Run Code

The continue statement in Python is the opposite of the break statement. It forces the execution of the next iteration instead of terminating the loop.

The Python continue statement rejects the rest of the statements in the current iteration of the loop. Then the control l is moved back to the top of the loop. Continue statements can be used in both for and whole loops. The following example will help you understand better.

Execution Flow for Python Continue Statement

for loop

    #code block
    statement 1
    statement 2
    .
    .
    .
    if condition:
        continue
    statement 3
    statement 4
    .
    .
    .
    #remaining code block
You can also try this code with Online Python Compiler
Run Code

Flow Chart For Python Continue Statement

Flow Chart For Continue Statement

Examples of Python Continue Statement 

Now that we have a better jist of the continue statement, we move on to implementing the continue statement to understand further.

Implementation in for loop

for letter in 'CodingNinjas':    
   if letter == 'N':
      continue
   print (' ', letter)
You can also try this code with Online Python Compiler
Run Code

 

Output
  C
  o
  d
  i
  n
  g
  i
  n
  j
  a
  s
You can also try this code with Online Python Compiler
Run Code

 

Explanation

In the above program, we use the string 'CodingNinjas' to check for the letter N. If the letter is N, the program skips and executes the following statement for the letter 'not N'.

Implementation in while loop

i = 0
while i <= 7:    
    if i == 4:
        i += 1
        continue  
    print("" , i)
    i += 1
You can also try this code with Online Python Compiler
Run Code

 

Output
 0
 1
 2
 3
 5
 6
 7
You can also try this code with Online Python Compiler
Run Code

 

Explanation

In the above program, if  'i = 4', we use the continue statement to skip to the rest of the iterations in the program inside the while loop. If 'i' is not equal to 4, the number is presented and incremented by 1.

Advantages Of Using Continue Statements

  • Continue statements make your code more reliable and readable by skipping the conditions based on specific conditions.
     
  • It is a versatile tool that can be incorporated into the use of for and while loops.
     
  • Complex programs take the most advantage out of the continued statements. It customises the manner of the code by specifying the lines or iterations to be skipped in the loops.

Pass Statement in Python

pass
You can also try this code with Online Python Compiler
Run Code

The statements, Break Continue Pass in Python break or make the program. After perfecting the break and continue statement, we move on to perfecting the Pass Statement in Python.

A pass statement in Python is used to do "nothing". It is a null statement that returns no operation. Occasionally, a pass statement plays the role of a placeholder when implementing new methods.

The pass statement is generally not that useful in Python and does not make a difference in the code except for making it shorter.

Execution Flow for Pass Statement

for element in sequence:
    if condition:
        pass
You can also try this code with Online Python Compiler
Run Code

Implementation of Pass Statement

num = 100


if num < 10:
    pass


print('This is an example of Pass Statement')
You can also try this code with Online Python Compiler
Run Code

 

Output

This is an example of Pass Statement
You can also try this code with Online Python Compiler
Run Code

Advantages Of Using Pass Statements

  • The pass statement is used as a placeholder when the program does not yet have any functionality or it may be in the construction phase.
     
  • Allows to write code that compiles without providing an exact implementation.
     
  • For instance, including at least one method is necessary when creating a new class. The pass statement allows you to create an empty method without initiating any syntax error.
     
  • The pass statement is used to improve the readability of the program code.

Also read,  Convert String to List Python

Difference Between Continue and Pass in Python

Continue Statement

Pass Statement

The continue statement is used for skipping the rest of the statements in the current iteration.

The pass statement is used as a ‘place-holder’ and returns nothing.
After execution, the control is returned to the start of the loop.There is no change of control in the pass statement. It is considered as a null statement; hence execution proceeds in the usual way.
The continue statement can be used in both for and while loops.The pass statement can be used in loops for declaring an empty function or an empty class.
Usually, the continue statement is used inside a loop condition.The pass statement is discarded.

Some Common Mistakes To Avoid While Using Break Continue and Pass In Python

Here are some of the common mistakes to avoid while using Break Continue Pass in Python:

  1. One of the most widely mistaken things in Python is indentation. The Break Continue Pass statements heavily rely on indentation. Syntax errors occur if not appropriately used.
     
  2. Overusing Break Continue Pass can lead to unexpected errors. The program is hard to read and understand if these loop control statements are incorporated in every loop.
     
  3. If, in any condition, a program faces an error in the loop containing Break Continue Pass in Python, it leads to the program not being able to exit the loop.

 

Also read - Pattern in Python

Frequently Asked Questions

What is break continue and pass in Python?

In Python, the keywords "break" and "continue" are used to escape a loop early, "pass" is a placeholder statement that does nothing, and these words are frequently used when a statement is syntactically necessary but no action is required

Why do Python developers use continue break and pass?

Statements like break, continue, and pass are used by Python developers to regulate the progression of their code. With the aid of these statements, loops can be made more effective, specific sections of a loop can be skipped, or a specific area of code can be marked as not yet implemented.

What type is pass in Python?

The pass statement has no value because it is a null statement. Usually, it serves as a placeholder for future code.

What is the difference between break continue and pass statements?

The break statement terminates the current loop or statement, the continue statement skips the remaining code inside a loop for the current iteration only, and the pass statement is a null statement that does nothing.

What is continue used for in Python?

The continue statement in Python is used to skip the current iteration of a loop and move to the next iteration without exiting the loop.

How do you skip or continue in Python?

To skip an iteration in Python, use the continue statement inside a loop. It bypasses the remaining code for that iteration and proceeds to the next.

Should you use Python continue?

Yes, use continue when you need to skip specific iterations in a loop, improving readability and controlling flow without breaking the entire loop.

Conclusion

In this article we covered Break Continue Pass in Python along with their syntax, execution flows, examples and advantages.

Do not forget to check out some recommended readings:

Hope this article helped you in your learning journey.

Do check out The Interview guide for Product Based Companies as well as some of the popular Interview problems from top tech companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also, Check out some of the amazing Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingBasics of C etc. along with some Contests and Interview Experiences only on Coding Ninjas Studio.

Live masterclass