Loop control statements - Break, Continue, Pass in Python

Break, Continue, Pass in Python:
  • As we know, in Python we have two types of the loop 'for' and 'while' which allows us to iterate over a list, tuple, string, dictionary, and set. 
  • But there are some cases where we want our loop to exit completely or skip a part of the loop or ignores some particular condition. 
  • In such cases, we will use these loop control statements. In Python, we have the below loop control statements
          • Break
          • Continue
          • Pass
Break:
  • The break statement is used to terminate from the current loop and control of the program continues with the execution of statements that are present after the loop/break statement.
  • In the case of nested loops (loop inside another loop), if the break statement is present in the inside loop, then the break statement will terminate only the innermost loop and control continues the execution with the outside loop.
Continue:
  • Continue statement is used to return the control to the beginning of the loop by skipping all the remaining statements inside the current iteration of the loop.
  • Loop continues on with the next iteration.
Pass:
  • Pass statement is used where your code requires a statement syntactically but has not been written yet.
  • Pass will act as a placeholder in your code where the python interpreter expects a statement.
  • Basically, the pass statement is like a null operation, it results in No Operation (NOP).

No comments:

Post a Comment