Explain break and continue statement with example
C provides two commands to control how we loop:
• break -- exit form loop or switch.
• continue -- skip 1 iteration of loop.
Consider the following example where we read in integer values and process them according to the following conditions. If the value we have read is negative, we wish to print an error message and abandon the loop. If the value read is great than 100, we wish to ignore it and continue to the next value in the data. If the value is zero, we wish to terminate the loop.
while (scanf( ``%d'', &value ) == 1 && value != 0) {
if (value < 0) {
printf(``Illegal value n'');
break;
/* Abandon the loop */
}
if (value > 100) {
printf(``Invalid value n'');
continue;
/* Skip to start loop again */
}
/* Process the value read */
/* guaranteed between 1 and 100 */
....;
....;
} /* end while value != 0 */