Break and continue in C
The break keyword allows the loop to terminate as soon as it is encountered in the loop, and the control will automatically goes to the first statement after the loop.
The break statement is used with decision making statement such as if...else.
Syntax of break statement:
break;
Flowchart of break statement:
Working of break statement:
Using while loop...
while (test case) { // codes if (condition of break) { break; } // codes }
Using for loop
for ( initialization , condition , update ) { // codes if (condition of break) { break; } // codes }
Example of break… statement:
Continue statement:
Continue statement is exactly the opposite of break statement, instead of getting out of the loop like break condition it forces the next iteration of the loop to be executed.
Syntax of continue:
continue;
Flowchart of continue statement:
Working of continue statement:
Using while loop...
while (test case) { // codes if (condition of continue) { continue; } // codes }
Using for loop...
for ( initialisation , condition , update ) { // codes if (condition of continue) { continue; } // codes }
Example of continue statement: