Break Statement in C
n any loop break statement is used to jump out of loop skipping the code below it without caring about the test condition.
It interrupts the flow of the program by breaking the loop and continues the execution of code which is outside the loop.
How does break statement works?
Structure of Break statement
In while loop
while (test_condition)
{
statement1;
if (condition )
break;
statement2;
}
In do…while loop
do
{
statement1;
if (condition)
break;
statement2;
}while (test_condition);
In for loop
for (int-exp; test-exp; update-exp)
{
statement1;
if (condition)
break;
statement2;
}
Now in above structure, if the test condition is true then statement 1 will be executed and again if the condition is true then the program will encounter break the statement which will cause the flow of execution to jump out of loop and statement 2 below if statement will be skipped.
Continue Statement in C
When used in while, do while and for loop, it skips the remaining statements in the body of that loop and performs the next iteration of the loop.
Unlike break statement, Continue statement when encountered doesn’t terminate the loop, rather interrupts a particular iteration.
How continue statement work?
Structure of Continue statement
In while loop
while (test_condition)
{
statement1;
if (condition )
continue;
statement2;
}
In do…while loop
do
{
statement1;
if (condition)
continue;
statement2;
}while (test_condition);
In for loop
for (int-exp; test-exp; update-exp)
{
statement1;
if (condition)
continue;
statement2;
}
Explanation
In the above structures, if the test condition is true then the continue statement will interrupt the flow of control, and the block of statement 2 will be skipped, however, iteration of the loop will be continued.