p

python tutorial - Python Break - learn python - python programming



What is Python Break?

  • Python break statement, it terminates the current loop and resumes execution at the next statement, just like the traditional break statement in C.
  • The most common use for break is when some external condition is triggered requiring a hasty exit from a loop.
  • The break statement can be used in both while and for loops.
  •  python-break

    Learn Python - Python tutorial - python-break - Python examples - Python programs

  • Break statement is a jump statement that is used to pass the control to the end of the loop.
  • When break statement is applied the control points to the line following the body of the loop, hence applying break statement makes the loop to terminate and controls goes to next line pointing after loop body.
 python break statement

Example:

  • Use of break statement inside loop
for val in "string":
    if val == "i":
        break
    print(val)

print("The end")

Output:

s
t
r
The end
  • In this program, we iterate through the "string" sequence.
  • We check if the letter is "i", upon which we break from the loop.
  • Hence, we see in our output that all the letters up till "i" gets printed.
  • After that, the loop terminates.

Wikitechy tutorial site provides you all the learn python , online python course with certificate , python coding course , python scripting course python programming course online free

Related Searches to Python Break