[go: up one dir, main page]

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

Control Statement Notes

The document discusses Python loop control statements like break and continue. It provides examples of how break terminates the loop entirely, while continue skips the remaining statements and moves to the next iteration. It also explains the "else" clause that can be used with for and while loops, which executes if the loop terminates normally via failing conditions rather than by breaking. Nested loops are discussed as loops within loops, and an example shows how to print patterns using nested for loops.

Uploaded by

Sourav Chhetry
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)
20 views6 pages

Control Statement Notes

The document discusses Python loop control statements like break and continue. It provides examples of how break terminates the loop entirely, while continue skips the remaining statements and moves to the next iteration. It also explains the "else" clause that can be used with for and while loops, which executes if the loop terminates normally via failing conditions rather than by breaking. Nested loops are discussed as loops within loops, and an example shows how to print patterns using nested for loops.

Uploaded by

Sourav Chhetry
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

CONTROL STATEMENT

Jump Statements – break & continue break statement in


python is used to terminate the containing loop for any
given condition. Program resumes from the statement
immediately after the loop Continue statement in python
is used to skip the statements below continue statement
inside loop and forces the loop to continue with next
value.
Example – break
for i in range(1,20):
if i % 6 == 0:
break
print(i)
print(“Loop Over”)
The above code produces output
1
2
3
4
5
Loop Over
when the value of i reaches to 6 condition will becomes
True and loop will end and message “Loop Over” will be
printed.

Example – continue
for i in range(1,20):
if i % 6 == 0:
continue
print(i, end = „ „)
print(“Loop Over”)
The above code produces output
1 2 3 4 5 7 8 9 10 11 13 14 15 16 17 19
Loop Over
when the value of i becomes divisible from 6, condition
will becomes True and loop will skip all the statement
below continue and continues in loop with next value of
iteration.

Loop .. else .. Statement


Loop in python provides else clause with loop also which
will execute when the loop terminates normally i.e. when
the test condition fails in while loop or when last value is
executed in for loop but not when break terminates the
loop.
for with “else” SYNTAX

for <var> in <seq>:


statement 1
statement 2
else:
statement(s)

while with “else” SYNATX

while <test condition>:


statement 1
statement 2
else:
statement(s)

Example (“else” with while)


i=1
while i<=10:
print(i)
i+=1
else:
print("Loop Over")

Example (“else” with for)


names=["allahabad","lucknow","varanasi","kanpur","agra
","ghaziabad","mathura","meerut"]
city = input("Enter city to search: ")
for c in names:
if c == city:
print(“City Found")
break
else:
print("Not found")

Infinite loop and „break‟


We can also execute infinite loop purposely by pecifying
an expression which always remains True. However in
loop we can mention any condition to exit or terminate
loop
a=2
while True:
print(a)
a*=2
if a>100:
break

Nested Loop (loop within loop)


A loop may contain another loop in its body. This form of
loop is called nested loop. In Nested loop, the inner loop
will execute for each value of outer loop. For e.g. if the
outer loop is of 4 times and inner loop is of 5 times then
statement will execute 4 x 5 = 20 times.

Syntax of Nested Loop

Nested Loop – FOR


for i in sequence:
for j in sequence:
Statement1
Statement2
Nested Loop – WHILE

while condition:
while condition:
Statement1
Statement2

Example – Program to print the following patterns

You might also like