Python Loop Control Statements

In this tutorial you will learn about the Python Loop Control Statements and its application with practical example.

Python Loop Control Statements

In Python, loops statements gives you a way execute the block of code repeatedly. But sometimes, you may want to exit a loop completely or skip specific part of a loop when it meets a specified condition. It can be done using loop control mechanism.

In Python, you have loop control statements that can be used to alter or control the flow of loop execution based on specified conditions. In Python we have following loop control statements –

  • Break Statement
  • Continue Statement
  • Pass Statement

Python break statement

In Python, break statement inside any loop gives you way to break or terminate the execution of loop containing it, and transfers the execution to the next statement following the loop.

Syntax:-

Example:-

In this above program, the variable count is initialized as 0. Then a while loop is executed as long as the variable count is less than 10.

Inside the while loop, the count variable is incremented by 1 with each iteration (count = count + 1).

Next, we have an if statement that checks the variable count is equal to 5, if it return TRUE causes loop to break or terminate.

Within the loop there is a print() statement that will execute with each iteration of the while loop until the loop breaks.

Then, there is a final print() statement outside of the while loop.

When we run this code, our output will be as follows –

Output:-

Python continue statement

The continue statement gives you way to skip over the current iteration of any loop. When a continue statement is encountered in the loop, the python interpreter ignores rest of statements in the loop body for current iteration and returns the program execution to the very first statement in th loop body. It does not terminates the loop rather continues with the next iteration.

Syntax:-

Example:-

Output:-

As you can see when ctr == 5, continue statement is executed which causes the current iteration to end and the control moves on to the next iteration.

Python pass statement

In Python , the pass statement is considered as no operation statement, means it consumes the execution cycle like a valid python statement but nothing happens actually when pass is executed.The pass statement is much like a comment, but the python interpreter executes the pass statements like a valid python statements, while it ignores the comment statement completely. It is generally used to indicate “null” or unimplemented functions and loops body.

Syntax:-

Example:-

Output:-

 

 

 

In this tutorial we have learn about the Python Loop Control Statements and its application with practical example. I hope you will like this tutorial.