alternative
  • Home (current)
  • About
  • Tutorial
    Technologies
    SQL -Structured Query Language
    Python
    Ethical Hacking
    Java
    .net Framework
    Placement Preparation
    Quantitative Aptitude
    View All Tutorial
  • Quiz
    SQL -Structured Query Language
    Quantitative Aptitude
    Java
    View All Quiz Course
  • Q & A
    Quantitative Aptitude
    Java
    View All Q & A course
  • Programs
  • Articles
    Artificial Intelligence & Machine Learning Project
    How to publish your local website on github pages with a custom domain name?
    How to download and install Xampp on Window Operating System ?
    How To Download And Install MySql Workbench
    How to install Pycharm ?
    How to install Python ?
    How to download and install Visual Studio IDE taking an example of C# (C Sharp)
    View All Post
  • Tools
    Program Compiler
    Sql Compiler
    Replace Multiple Text
    Meta Data From Multiple Url
  • Contact
  • User
    Login
    Register

Python - Decision control and looping statement - Break Statement Tutorial

The break statement is used to stop the loop even if the condition is true. It is used to come out of the loop, whenever we need it for a given condition.

In the case of a nested loop, it will stop/break the inner loop first, after that it will also stop/break the outer loop.

Break statement can be performed in any loop ( for , while).

Syntax-

break;

 

Break statement in for loop

Example-

# List of number
number = [4, 1, 2, 27, 25, 73]
# iterate over the list of number
for val in number:
    if(val==27):
        break;
    print(val)

Run Program

Output-

4
1
2

 

In above program, if val is 27 it will stop/break whole loop and will not print the next number i.e [25, 73] .

While in continue statement, it will skip val = 27 (current iteration) and print the next number i.e [25, 73] .

So, for continue statement output will be –

4
1
2
25
73

 

Break statement in while loop

Example-

i=1
while i <= 6:
    print(i)
    if(i==3):
        break;
    i=i+1

 Run Program

Output-

1
2
3

 

Nested Loop

Without break statement-

# a for loop will executed 3 time i.e from 1 to 3
# b for loop will executed 3 time i.e from 2 to 4
for a in range(1,4):
    for b in range(2,5):
        print("a="+str(a)+" b="+str(b))

Run Query

Output-

a=1 b=2
a=1 b=3
a=1 b=4
a=2 b=2
a=2 b=3
a=2 b=4
a=3 b=2
a=3 b=3
a=3 b=4

 

With break statement-

# a for loop will executed 3 time i.e from 1 to 3
# b for loop will executed 3 time i.e from 2 to 4
for a in range(1,4):
    for b in range(2,5):
        if(b==3):
            break;
        print("a="+str(a)+" b="+str(b))

Run Program

Output-

a=1 b=2
a=2 b=2
a=3 b=2

In the above program, whenever in the inner loop b become 3, break statement will stop the inner loop, and come out of the inner loop, after that it will also stop the outer loop.

Python

Python

  • Introduction
  • Installation and running a first python program
    • How to install Python ?
    • Running 1st Hello World Python Program
    • How to install Pycharm ?
  • Things to know before proceed
    • Escape Characters/Special Characters
    • Syntax ( Indentation, Comments )
    • Variable
    • Datatype
    • Keyword
    • Literals
    • Operator
    • Precedence & Associativity Of Operator
    • Identifiers
    • Ascii, Binary, Octal, Hexadecimal
    • TypeCasting
    • Input, Output Function and Formatting
  • Decision control and looping statement
    • if-else
    • for loop
    • While loop
    • Break Statement
    • Continue Statement
    • Pass Statement
  • Datatype
    • Number
    • List
    • Tuple
    • Dictionary
    • String
    • Set
    • None & Boolean
  • String Method
    • capitalize()
    • upper()
    • lower()
    • swapcase()
    • casefold()
    • count()
    • center()
    • endswith()
    • split()
    • rsplit()
    • title()
    • strip()
    • lstrip()
    • rstrip()
    • find()
    • index()
    • format()
    • encode()
    • expandtabs()
    • format_map()
    • join()
    • ljust()
    • rjust()
    • maketrans()
    • partition()
    • rpartition()
    • translate()
    • splitlines()
    • zfill()
    • isalpha()
    • isalnum()
    • isdecimal()
    • isdigit()
    • isidentifier()
    • islower()
    • isupper()
    • isnumeric()
    • isprintable()
    • isspace()
    • istitle()
  • Python Function
    • Introduction
  • Lambda Function
    • Lambda Function
  • Python Advanced
    • Iterators
    • Module
    • File Handling
    • Exceptional Handling
    • RegEx - Regular Expression
    • Numpy
    • Pandas

About Fresherbell

Best learning portal that provides you great learning experience of various technologies with modern compilation tools and technique

Important Links

Don't hesitate to give us a call or send us a contact form message

Terms & Conditions
Privacy Policy
Contact Us

Social Media

© Untitled. All rights reserved. Demo Images: Unsplash. Design: HTML5 UP.