break and continue Statements
The break statement is used to alter the flow of control. When a break statement is executed in a while loop, for loop, do-while loop or switch statement, it causes immediate exit from that statement. Program execution continues with the next statement. Common uses of the break statement are to escape early from a loop or to skip the remainder of a switch statement.
The program written below demonstrates the break statement in a for-loop.
When the if-statement detects that x has become 5, break statement is executed. This terminates the for-loop and the program continues from cout after the for-loop.
The continue statement is also used to alter the flow of control. When it is executed in a while loop, for loop or do-while loop, it skips the remaining statements in the body of the control loop and performs the next iteration of the loop.
An example of continue statement is shown below,
Some programmers feel that break and continue statements violate the norms of structured programming since the effects of these statements can be achieved by structured programming technique. The break and continue statements, when used properly, perform faster than the corresponding structured technique.