Home » Python Tutorial » Python Pass, Break and Continue

Python Pass, Break and Continue


In this tutorial, we will learn about Python Pass, Break and Continue statements.

Python Pass

“pass” statements are used with if and else conditions. “pass” statement means no operation.

“if” conditions cannot be empty, but for some reason, we need to have an “if” condition with no content, we can use the “pass” statement to avoid getting an error.

a=5
b=2
if a>b:
  pass

Sometimes we don’t need the else statements at all. But to maintain the coding standard, we can add an else condition along with the “pass” statement.

a=5
b=2
if a>b:
  print("a is greater")
else:
  pass

Python Break

“break” statement is used along with for and while loops.

With the “break” statement we can stop the while and for loop, though the condition given in the statement is true.

i=1
while(i<=10):
  print(i)
  if i==5:
    break
  i+=1

In the above code, though the while statement checks for the variable until it becomes greater than 10, we are breaking the while loop the variable becomes 5.

Python Continue

Like “break” statement, “continue” statement also used along with for and while loops.

With the “continue” statement we can stop the current iteration alone and continue with the next iterations.

i=0
while(i<=5):
  i+=1
  if i==3:
    continue
  print(i)

In the above code, ‘3’ is not getting printed because we are skipping further steps when the variable becomes 3.

Conclusion

Even though we are giving the “break” and “continue” statements inside the “if” condition, it will be applicable only to the “while” and “for” loop is written immediately above the “if” condition.

Reference

Python Tutorial