Sitemap

When we can use a break and continue to the Looping Statement in Python

3 min readJun 5, 2023

--

Press enter or click to view image in full size
Photo by Mohammad Rahmani on Unsplash

What is a Looping Statement?

Looping statements are structures in programming that are used to repeatedly repeat a block of code. We can execute a series of statements repeatedly by using looping statements until a certain condition is met or a certain number of iterations are reached.
Several types of looping statements are commonly used in Python, such as for loops and while loops. Both have different syntax and usage, but their purpose is the same, which is to loop to execute a specific block code repeatedly.

The difference between For and While looping statements.

There are 2 types of loops in Python:

  1. For Statement

Used when you want to run and repeat a block of code for each element in a sequence and usually with a sequence data type (tuple, list, string, or range) or an object that can be repeated a certain number of iterations. The number of iterations in a for loop can be determined by the length of the sequence or object being repeated.

The for loop is used when the number of iterations is known.

Syntax:

For variable_name in element/object:

# Block of code to be executed

…

variable_name: is used to store each element in a repeated sequence/object.

in: pointing will perform repetition of each element in a specific order/object.

colon (:): The colon operator is used to notify about starting a code block.

element: sequence or object to be repeated (sequence data type)

Block Code: A code block can contain one or more statements.

Example :

fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)

2. While Statement

A looping function in Python programming is used to repeat a code block when a given condition is met. In a while loop, the code block will continue to repeat as long as the given condition is True.

Get Khanza Primazky’s stories in your inbox

Join Medium for free to get updates from this writer.

While loops are used when the number of previous iterations is unknown.

Syntax:

While condition:

# Block of code to be executed

…

At the beginning of the iteration, the condition will be checked. If the condition is True, then the code block in the while loop will be executed. After the execution, the program will return to the beginning of the while loop and check the condition again. If the condition is still True, the code block will be executed again. This process will continue until the condition is False.

Example:

count = 0
while count < 5:
print(count)
count += 1

Break Statement

A statement used in a loop (be it a for loop or a while loop) to forcefully stop the execution of the loop and exit the loop.

  • Should break statements be used?

No, break statements are not required in every loop.

The use of breaks depends on the needs of the program being built. In many cases, Break is used to stop the loop if a certain condition is met or the desired result has been obtained.

Example:

while True:
user_input = input("Enter a number (0 for exit): ")
if user_input == "0":
print("exit the loop.")
break
print("ou entered a number:", user_input)

Continue Statement

A statement in Python that is used to skip the remaining block of code in a loop when a certain condition is met and continue with the next iteration. When the continue statement is executed, the program skips the remaining blocks of code left in the current iteration and goes directly to the next iteration in the loop.

Example:

while True:
name = input("Enter name (or 'e' to exit): ")
if name == 'e':
print("Program completed.")
break
elif not name.isalpha():
print("Invalid input. Enter name or 'e' to exit.")
continue
print("Hello,", name)

Conclusion

  1. Break statement: When a break statement is executed in a loop (for or while), the loop is immediately terminated, and the program exits the loop. Program execution resumes on the line of code following the loop. In other words, the break statement is used to forcefully stop the loop when certain conditions are met.
  2. Continue statement: When a continue statement is executed in a loop, the current iteration is stopped, and the program will proceed to the next iteration in the loop. In other words, the continue statement is used to skip the rest of the code in the current iteration and proceed to the next iteration. The loop continues to run and is not terminated.

--

--