The main distinction between a break and a continue statement in the C programming language is that a break causes the closest enclosing loop or switch to be instantly terminated. The next iteration of the enclosing for, while, or do loop starts when the continue command is executed. While the for loop moves the control to the loop’s increment step, the continue statement in a while and create loops instantly moves the control to the loop’s test condition.
What is the Break statement in C?
The break statement is a loop control statement used to end a loop in C or C++. The loop iterations close as soon as the break statement is encountered from within the loop, and control is instantly transferred from the loop to the first statement following the loop. Break statements are generally used when unsure of the precise number of loop iterations to perform or when we wish to end the loop based on a condition.
Example of Break Statement
Whenever the top right non-blank, non-tab, non-newline is located, the trim function uses a break to escape from a loop and eliminates the trailing blanks, tabs, and newlines from the end of a string. The size of the string is returned. The first letter that is not a blank tab or relatively more recent is what the for loop looks for as it scans backward from the end of the string. The loop is broken when one is discovered or when n is negative.
Now let’s examine the break statement-based examples for the three loops:
- Simple loops:
Let’s say we want to find a specific element in an array. Use a loop to iterate through the array starting at the first index, then compare the array members with the provided key to accomplish this.
- Nested Loops:
If the break statement is utilised in the core loop, we may also utilise break statements while interacting with nested loops. Only the innermost loop will release the control.
Output –
- Infinite Loops:
With a condition to stop the endless loop’s execution, the break statement can be used in an infinite loop.
What is the continue statement in C?
Like the break statement, continue is a loop control statement. As its name implies, the continue statement compels the loop to continue or carry out the subsequent iteration. The code inside the loop that comes after the continue statement is skipped when the continue statement is used in a loop, and the next iteration of the loop then starts.
Breaking the loop’s remaining iterations and allowing the control to leave the loop are known as the continue statement. The continue statement switches control to the next iteration and halts the remaining loop code execution for the current iteration.
Example of Continue Statement:
Think about a scenario where you must create software that only prints the digits 1 through 10, not the number 6. It is said that you must create this using loop and that you may use only one loop at a time. Here is where the continue statement is used. We can execute a loop from 1 to 10 times, comparing the value of the iterator each time with 6. The continue statement will allow us to move on to the next iteration without publishing anything if it equals 6, but otherwise, we will print the value.
Difference between the Break and continue statement in C language:
Break Statement | Continue statement |
Switching and looping (for, while, do) statements can contain breaks. | Only statements that use a loop (for, while doing) may include a continue. |
The switch or looping statements are terminated immediately upon execution by a break. The break signals the abrupt termination of the loop or switch. | A continuation causes the loop to move on to the subsequent iteration rather than ending the loop. Even if continue is encountered, the loop iterations are all completed. The statements in the loop that follows the continue statement is skipped using the continue statement. |
Both switching and loop statements are compatible with the break stat. | Only loops are permitted to contain the continued expression. You will receive an error if you include this in a switch statement. |
When encountered, a break statement ends the block and removes control from the switch or loop. | A continue statement advances the loop’s control to the following iteration when it is encountered. |