Transfer Statements

Transfer Statements

Transfer statements alter the way a logic gets executed. These statements are often used in loops for and while. Let’s have a look at the transfer statements below.

1. break

We can use break statement inside loops to break loop execution based on some condition.

for i in range(10): 
    if i==7: 
        print("Processing is enough..please break the loop")
        break
    print(i)

Output

0
1
2
3
4
5
6
Processing is enough..please break the loop

E.g 2

cart=[10,20,600,60,70] 
for item in cart: 
    if item>500:
        print("To place this order insurence must be required")
        break
    print(item)

Output

10
20
To place this order insurence must be required
2. continue

We can use continue statement to skip current iteration and continue next iteration

E.g: To print odd numbers in the range 0 to 9

for i in range(10):
    if i%2==0: 
        continue 
    print(i)

Output

1
3
5
7
9

Loops with else Block

  • Inside loop execution, if break statement not executed, then only else part will be executed.
  • else means loop without break.
cart=[10,20,30,40,50]
for item in cart: 
    if item>=500: 
        print("We cannot process this order")
        break
    print(item) 
    else: 
        print("Congrats...all items processed successfully")

Output

10
20
30
40
50
Congrats...all items processed successfully
3. pass statement
  • pass is a keyword in Python.
  • In our programming syntactically if block is required which won’t do anything then we can define that empty block with pass keyword.
  • This statement does nothing. It is used to define an empty block of code or a class. When written in a loop statement, it’s usually the last statement.

E.g

numbers = [10,11,12,13,14]
for num in numbers :
    if num%2 == 0:
        print(num)
    else
        pass
Transfer Statements


Scroll to top