How to use break and continue respectively based on the requirment

Break and continue

While executing the loop if we want to break or skip the current loop and want to move to the next then we can use break and continue respectively based on the requirement.


Break statement

If the user adds a break then execution of the program/loop stops and the cursor will come out of the loop or current block.

Example: if we want to break the loop when i = 5.

In the below example since we are breaking the loop if i = 5, the output will be printed only until i is 4, and the loop will be terminated when “i” becomes 5.

Break and continue

Continue statement

Instead of breaking the loop if we want to skip the current loop execution and want to move to the next value then we can use continue in the system.

In the above example if we replace the “break” with “continue” then when the value of “i” becomes 5 that loop execution will be skipped and the next value loop will be considered. Hence in the output “Value of i: 5” this line will be skipped, and 1 to 10 values will be printed.

Example:

Break and continue

-A blog by Shwetali Khambe

Related Posts