Added an option to terminate an iteration when a condition is true