0% found this document useful (0 votes)
14 views6 pages

I-NOTES (Loop Control Statement)

Uploaded by

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

I-NOTES (Loop Control Statement)

Uploaded by

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

Loop control statement

 Loop control statements change execution from its normal sequence.


 When execution leaves a scope, all automatic objects that were created in that scope
are destroyed.
 Python supports the following control statements,
 Break statement
 Continue statement
 Pass statement

Use of break and continue in Python

 In Python, break and continue statements can alter the flow of a normal loop.
 Loops iterate over a block of code until the test expression is false, but sometimes we
wish to terminate the current iteration or even the whole loop without checking test
expression.

Python break statement

 The break statement terminates the loop containing it.


 Control of the program flows to the statement immediately after the body of the loop.
 If the break statement is inside a nested loop (loop inside another loop), the
break statement will terminate the innermost loop.
Syntax:
break

Flowchart of break
The working of break statement in for loop and while loop

Example:

for val in "string":

if val == "i":
break

print(val)

print("The end")

Here,

 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:

Continue
Flowchart of continue

The working of continue statement in for and while loop


Example:

for val in "string":

if val == "i":

continue

print(val)

print("The end")

Here,

 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.
 Python pass statement
 In Python programming, the pass statement is a null statement.
 The difference between a comment and a pass statement in Python is that while the
interpreter ignores a comment entirely,pass is not ignored.
 However, nothing happens when the pass is executed.
 It results in no operation (NOP).

Syntax:

Pass

Important:

 we have a loop or a function that is not implemented yet, but we want to implement it
in the future.
 They cannot have an empty body.
 The interpreter would give an error.
 Here, we use the pass statement to construct a body that does nothing.

Example:

sequence = {'p', 'a', 's', 's'}

for val in sequence:


pass

You might also like