Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

Python_break_and_continue

This document provides an overview of the break and continue statements in Python, explaining their usage in altering the flow of loops. The break statement terminates the loop, while the continue statement skips the current iteration and continues with the next one. Examples are provided to illustrate how each statement works within for and while loops.

Uploaded by

chidziwosteven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python_break_and_continue

This document provides an overview of the break and continue statements in Python, explaining their usage in altering the flow of loops. The break statement terminates the loop, while the continue statement skips the current iteration and continues with the next one. Examples are provided to illustrate how each statement works within for and while loops.

Uploaded by

chidziwosteven
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

JAVA | C TUTORIAL | C++ | KOTLIN | SWIFT | R TUTORIAL

TUTORIALS EXAMPLES BUILT-IN FUNCTIONS GET APP 

Contents
PYTHON TUTORIAL Python break and continue What is the use of break
and continue in Python?

Python Introduction In this article, you will learn to use break and continue statements to alter Python break statement
the ow of a loop.
Python Flow Control Syntax of break

-- Python if...else Flowchart of break

-- Python for Loop Example of break


What is the use of break and continue in Python?
-- Python while Loop Python continue statement
In Python, break and continue statements can alter the ow of a normal loop.
-- Python break and continue Syntax of Continue

-- Python Pass Loops iterate over a block of code until test expression is false, but Flowchart of continue

sometimes we wish to terminate the current iteration or even the whole loop Example: Python
-- Take Quiz
without checking test expression. continue

Python Functions The break and continue statements are used in these cases.
Python Datatypes
Python Files
Python Object & Class Python break statement
Python Advanced Topics The break statement terminates the loop containing it. Control of the program
Python Date and time ows to the statement immediately after the body of the loop.

If break statement is inside a nested loop (loop inside another loop), break
will terminate the innermost loop.

Syntax of break
break

Flowchart of break

Receive the latest tutorial


to improve your
programming skills.

Enter Email Address* Join

The working of break statement in for loop and while loop is shown below.

RECOMMENDED READINGS

Python while Loop

Python for Loop

Python pass statement

Python if...else Statement

Python Statement, Indentation and


Comments

Example: Python break


script.py IPython Shell
1 # Use of break statement inside loop
2
3 for val in "string":
4 if val == "i":
5 break
6 print(val)
7
8 print("The end")

Run 

Powered by DataCamp

Output

s
t
r
The end

In this program, we iterate through the "string" sequence. We check if the


letter is "i" , upon which we break from the loop. Hence, we see in our
output that all the letters up till "i" gets printed. After that, the loop
terminates.

Python continue statement


The continue statement is used to skip the rest of the code inside a loop for
the current iteration only. Loop does not terminate but continues on with the
next iteration.

Syntax of Continue
continue

Flowchart of continue

The working of continue statement in for and while loop is shown below.

Example: Python continue


script.py IPython Shell
1 # Program to show the use of continue statement inside loops
2
3 for val in "string":
4 if val == "i":
5 continue
6 print(val)
7
8 print("The end")

Run 

Powered by DataCamp

Output

s
t
r
n
g
The end

This program is same as the above example except the break statement has
been replaced with continue.

We continue with the loop, if the string is "i" , not executing the rest of the
block. Hence, we see in our output that all the letters except "i" gets
printed.

« PREVIOUS
PYTHON WHILE LOOP PYTHON PASS STATEMENT
NEXT
»

TUTORIALS EXAMPLES COMPANY LEGAL

Python Tutorials Python Examples About Privacy Policy

C Tutorials C Examples Advertising Terms And Conditions

Java Tutorials Java Examples Contact App's Privacy Policy

Kotlin Tutorials Kotlin Examples App's Terms And Conditions

C++ Tutorials C++ Examples

Swift Tutorials R Examples

R Tutorials

Algorithms Tutorials

Copyright © Parewa Labs Pvt. Ltd. All rights reserved.

You might also like