All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To End A For Loop In Python

Last Updated : Mar 11, 2024

How To End A For Loop In Python

In this article we will show you the solution of how to end a for loop in python, the "for" loop in Python is frequently used to iterate over a listing of elements or to carry out a fine range of iterations.

In some occasions, despite the fact that, you may want to end the loop before all of its iterations are whole.

You can use the "break" statement, to perform this. When encountered, the "break" statement, that is placed inside the loop's frame, right now ends the execution of the loop and passes control to the subsequent sentence outside the loop.

This offers you the choice to go away the loop early based on a positive need or scenario. Now move to the concept of how to end a for loop in python.

Step By Step Guide On How To End A For Loop In Python :-

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for number in numbers:
    print("Current number:", number)
    if number == 5:
        print("Reached the number 5. Exiting the loop.")
        break
    print("Continuing to the next iteration.")
print("Loop ended.")
  1. Initialising a list called numbers, which comprises integers ranging from 1 to 10, is the first step.
  2. Each item in the numbers list is iterated over using the for loop. In each cycle, it gives a different variable number to each element.
  3. We have a print statement inside the loop that prints the current number.
  4. The message "Current number:" will be printed as the loop continues, followed by the value of the number variable.
  5. Next, an if declaration is used to determine whether the current number is same to 5.
  6. If the condition is True, it indicates that we've got arrived at the number five.
  7. The if statement executes the code block inside if the condition is satisfied.
  8. The words "Reached the number 5" are printed. cutting the loop off.
  9. This notification indicates that we have reached number 5 and are getting ready to leave the loop.
  10. The loop is then instantly ended by using the break statement.
  11. The programme exits the loop when it encounters the break statement and moves on to the next statement outside the loop.
  12. There is another print statement that appears after the break statement that says, "Continuing to the next iteration."
  13. The result, however, can only be drawn if the current value is less than 5.
  14. It offers feedback to show that the loop is still executing iterations.
  15. The code keeps executing the statement outside of the loop whenever the loop has finished all of its iterations or comes across the break statement. Here, it displays the message "Loop ended."

Conclusion :-

As a result, we were able to understand the idea of how to stop a for loop in Python.

We also discovered how effective the Python "break" command is at abruptly ending for loops.

It enables you to regulate the program's flow and end the loop in response to certain circumstances.

You can effectively halt the loop and go to the following statement outside the loop's body by using the "break" command.

I hope this article on how to end a for loop in python helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪