Overview
| 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:
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
Description:
Syntax:
📌 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)
🔍 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 |