Break and Continue Statements in Python: Python provides break and continue statements to control the execution of loops. These statements are especially useful when working with large datasets, performing repetitive calculations, or processing experimental and clinical data.

1. Break Statements in Python
The break statement is used to immediately terminate a loop. When Python encounters break, it exits the current loop and continues executing the code after the loop.
Syntax
for variable in sequence:
if condition:
break
Example: Using break in a for Loop
for i in range(1, 11):
if i == 6:
break
print(i)
Output:
1
2
3
4
5
Explanation:
The loop starts from 1 and continues until 10. However, when i becomes 6, the break statement terminates the loop immediately. Therefore, only values from 1 to 5 are printed.
2. Break Statement in Pharmaceutical Applications
The break statement can be useful when processing pharmaceutical or laboratory data.
Example: Stopping When an Invalid Reading Is Found
temperature_readings = [25, 26, 27, 28, -5, 29, 30]
for temperature in temperature_readings:
if temperature < 0:
print(“Invalid temperature detected. Process stopped.”)
break
print(“Temperature:”, temperature)
Output:
Temperature: 25
Temperature: 26
Temperature: 27
Temperature: 28
Invalid temperature detected. Process stopped.
Explanation:
The program checks each temperature reading. When an invalid value (-5) is detected, the loop stops using break.
3. Continue Statement
The continue statement is used to skip the current iteration of a loop and move directly to the next iteration.
Unlike break, the continue statement does not terminate the entire loop.
Syntax
for variable in sequence:
if condition:
continue
Example: Using continue in a for Loop
for i in range(1, 11):
if i == 6:
continue
print(i)
Output:
1
2
3
4
5
7
8
9
10
Explanation:
When i becomes 6, the continue statement skips the print() statement for that iteration. The loop then continues with 7, 8, 9, and 10.
4. Continue Statement in Pharmaceutical Applications
Suppose we have drug concentration values, but some values are missing or invalid. We can use continue to skip those values.
drug_concentrations = [10.5, 12.3, -1, 15.2, 18.6, -1, 20.1]
for concentration in drug_concentrations:
if concentration < 0:
print(“Invalid value skipped”)
continue
print(“Drug concentration:”, concentration)
Output:
Drug concentration: 10.5
Drug concentration: 12.3
Invalid value skipped
Drug concentration: 15.2
Drug concentration: 18.6
Invalid value skipped
Drug concentration: 20.1
Explanation:
Whenever a negative value is found, the program considers it invalid and skips that iteration. The remaining valid values are processed normally.
5. Difference Between Break and Continue
| Feature | break | continue |
| Main purpose | Terminates the loop | Skips the current iteration |
| Effect on loop | Stops the entire loop | Continues with the next iteration |
| Execution after statement | Moves outside the loop | Returns to the beginning of the next iteration |
| Useful when | Further processing is unnecessary | Only specific data should be ignored |
Simple Example
# break example
for i in range(1, 6):
if i == 3:
break
print(i)
Output:
1
2
# continue example
for i in range(1, 6):
if i == 3:
continue
print(i)
Output:
1
2
4
5
6. Using Break and Continue in a While Loop
Both statements can also be used with while loops.
Break Example
count = 1
while count <= 10:
if count == 6:
break
print(count)
count += 1
Output:
1
2
3
4
5
Continue Example
count = 0
while count < 10:
count += 1
if count == 6:
continue
print(count)
Output:
1
2
3
4
5
7
8
9
10
Important: When using continue in a while loop, make sure the loop-control variable is updated correctly. Otherwise, the program may enter an infinite loop.
7. Practical Pharmaceutical Example
Consider a quality-control program that checks the weight of tablets. If a critical error is detected, the process should stop. If only a missing value is found, that value can be skipped.
tablet_weights = [500, 498, None, 502, 499, 300, 501]
for weight in tablet_weights:
# Skip missing data
if weight is None:
print(“Missing value skipped”)
continue
# Stop if a critically abnormal value is found
if weight < 400:
print(“Critical abnormal weight detected. Testing stopped.”)
break
print(“Tablet weight:”, weight)
This example demonstrates how:
- continue skips missing data.
- break stops the process when a serious abnormality is detected.
Conclusion
The break and continue statements are important Python loop-control statements.
- break is used to exit a loop immediately.
- continue is used to skip the current iteration and continue with the next one.
For pharmacy and pharmaceutical science students, these statements can be useful when processing experimental data, laboratory readings, drug concentrations, patient datasets, quality-control parameters, and other scientific data. Understanding break and continue helps students write more efficient and flexible Python programs.
Editorial Note
This article has been carefully researched and written by Deepak Rajput with a focus on accuracy, clarity, and evidence-based healthcare information.






