0% found this document useful (0 votes)
30 views18 pages

Jumping Statements

Uploaded by

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

Jumping Statements

Uploaded by

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

JUMPING STATEMENTS

There are two jump statements to be used


within loops to jump out of loop iterations.
• break
• continue
break
 A break statement skips the rest of the loop
and jumps over to the statement following the
loop
 A break statement terminates the very loop it
lies within
 Execution resumes at the statement
immediately following the body of the
terminated statement.
Example for break
for val in "string":
if val == "i":
break
print(val)
print("The end")
output:
s
t
r
The end
var = 10
while var > 0:
print (“Current variable value :”, var)
var = var -1
if var == 5:
break
print ("Good bye!“)
OUTPUT:
Current variable value : 10
Current variable value : 9
Current variable value : 8
Current variable value : 7
Current variable value : 6
Good bye!
a=2
while True:
print(a)
a=a*2
if a>100:
break
For purposely created infinite loops, there must be
a provision to reach break statement from within
body of the loop so that loop can be terminated.
Continue
 The continue statement forces the next
iteration of the loop to take place , skipping
any code in between.
 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.
Example for continue
for val in "string":
if val == "i":
continue
print(val)
print("The end")
OUTPUT:
s
t
r
n
g
The end
print("Break statement Output")
for i in range(1,11):
if i%3==0:
break
else:
print(i)
print("Continue statement output")
for i in range(1,11):
if i %3==0:
continue
else:
print(i)
OUTPUT
Break statement Output
1
2
Continue statement output
1
2
4
5
7
8
10
LOOP ELSE STATEMENT
 Python allows the else keyword to be used
with the for and while loops too.
 The else block appears after the body of
the loop.
 The statements in the else block will be
executed after all iterations are completed.
 The program exits the loop only after
the else block is executed.
Example
for a in range(1,4):
print("element is",end=" ")
print(a)
else:
print("Ending loop")
output:
element is 1
element is 2
element is 3
Ending loop

You might also like