Overview

In Python, break and continue are loop control statements used to alter the flow of loops (for or while). Though both affect the loop, they behave differently:


Statement

Action

break

Exits the loop entirely

continue

Skips the current iteration only



break – Exit the Loop Immediately


Description:

The break statement terminates the entire loop once a specific condition is met. It prevents any further iterations.


Syntax:

for item in iterable:

    if condition:

        break


Realistic Odoo Example:

Loop through partners and stop at the first one that has a valid email:​


for partner in self.env['res.partner'].search([]):

    if partner.email:

        _logger.info("Found partner with email: %s", partner.name)

        break  # Stop the loop immediately


Use break when you want to exit after the first match.



continue – Skip Current Iteration


Description:

The continue statement skips the current loop iteration and moves to the next iteration without executing the remaining code inside the loop.


Syntax:

for item in iterable:

    if condition:

        continue

    # remaining loop code



Realistic Odoo Example:

Loop through partners and skip the ones without an email:​


for partner in self.env['res.partner'].search([]):

    if not partner.email:

        continue  # Skip if email is missing

    _logger.info("Processing partner: %s", partner.name)


✅ Use continue to ignore or filter specific records during looping.



🔍 Summary Table


Feature

break

continue

Loop behavior

Exits loop immediately

Skips current iteration, continues loop

Position in loop

Usually inside if condition

Same

Use case

Stop at first match

Skip/ignore unwanted values



✅ When to Use in Odoo


Use Case

Recommended Statement

Stop when first valid record is found

break

Skip processing for invalid/incomplete records

continue

Improve performance in record iteration

Both as needed