Example HTML page

Loop Control Statements

Loop Control Statements

The Loop control statements change the execution from its normal sequence.

Python supports the following loop control statements for both for and while loops

Control Statement

Description

break

It terminates the loop when the condition is not satisfied and shifts the control to the immediate statement of that loop.

continue

It executes until the condition satisfies and ignores the remaining statements and again starts the execution from beginning onwards.

pass

It is null operation. Nothing will happen when it executes.

 for loop using break statement

Ex 1: Sample example of for loop using break statement

for i in range(10):
    if i == 4:
        break
    print(i)

Output

0

1

2

3

Ex 2: The following program describes the use of break in a for loop iterating over a list. User gives a number as an input, and it is searched in the list. If it is found, then the loop terminates with the ‘found’ message otherwise displays a message “Not found”.

x=int(input("Number is:"))
L=[12,21,11,110,101,111,22]
for a in L:
    if a==x:
        print("Found")
        break
else:
        print("Not found")

Output

Number is: 10

Not found

Ex 3: The below code depicts the use of break statement inside a loop

for val in "string":
    if val=="i":
        break
    print(val)
print("The end")

Output

s

t

r

The end

If the string has repeated letters

for b in "PythonProgramming":
    if b=="m":
        break
    print(b)
print("EnD")

Output

P

y

t

h

o

n

P

r

o

g

r

a

EnD

 for loop using continue statement

Ex 1: A sample program on for loop using continue Statement

for i in range(10):
    if i == 4:
        continue
    print(i)

Output

0

1

2

3

5

6

7

8

9

 Ex 2: The following program shows the use of continue statement inside loops.

for val in "sudhakar":
    if val=="a":
        continue
    print(val)
print("The end")

Output

S

u

d

h

k

r

The end

 Ex 3 The below code depicts the use of continue statement inside a loop

for b in "PythonProgramming":
    if b=="m":
        continue
    print(b)
print("EnD")

 Output

P

y

t

h

o

n

P

r

o

g

r

a

i

n

g

EnD

 for loop using pass statement

Ex 1: A sample program on for loop using pass statement

for letter in 'Python':
    if letter=="h":
        pass
    print("This is pass block")
    print("Current letter:",letter)
print("BYe")

 Output:

This is pass block
Current letter: P
This is pass block
Current letter: y
This is pass block
Current letter: t
This is pass block
Current letter: h
This is pass block
Current letter: o
This is pass block
Current letter: n
BYe

Ex 2: Another snippet for pass statement

sequence={"P","y","t","h","o","n"}
for val in sequence:
    pass
print(sequence)

Output:

{‘P’, ‘y’, ‘h’, ‘n’, ‘o’, ‘t’}

Note:

When you execute the above code you will be displayed by random arrangement of a specific word. You can observe the same in the term “Python” with unordered arrangement.

Ex 3: The below code depicts the use of pass statement inside a loop

for b in "PythonProgramming":
    if b=="m":
        pass
    print(b)
print("EnD")
    

Output

P

y

t

h

o

n

P

r

o

g

r

a

m

m

i

n

g

EnD

While loop using break statement

Ex 1: To print the numbers in reverse order.

var=10
while var>0:
    print(var)
    var=var-1
    if var==5:
        break

Output

10

9

8

7

6

 Ex 2: The below code take input from the user and prints until the vowel is entered.

vowels="AEIOUaeiou"
while vowels:
    v=input("Enter a vowel: ")
    if v in vowels:
        break
    print("Not a vowel.")
print("Hey!!It's vowel")

 Output

Enter a vowel: t

Not a vowel.

Enter a vowel: f

Not a vowel.

Enter a vowel: o

Hey!!It’s vowel

 while using continue statement

Ex 1: a sample example on While using continue statement

var=10
while var>0:
    var=var-1
    if var==5:
        continue
    print(var)
print("End")

Output

9

8

7

6

4

3

2

1

0

End

Ex 2: To print all even numbers less than 10 and greater than or equal to 0.

x=10
while x:
    x=x-1
    if x%2!=0:
        continue
    print(x,end='  ')

 Output

8  6  4  2  0

 while using pass statement

Ex 1: To print 1 to 10 numbers in the following two different ways using pass statement.

i=1
while(i<=10):
    if(i==6):
         pass
    else:
        print(i)
    i=i+1

Output

1

2

3

4

5

7

8

9

10

Ex 2: To interrupt the flow with printing any string.

i=1
while(i<=10):
    if(i==6):
        print("Hello")
        pass
    else:
        print(i)
    i=i+1

Output

1

2

3

4

5

Hello

7

8

9

10

 

 

 

 

 

 

 

 

 

Example HTML page

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest