C provides two commands to control how we 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 greater 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;
/* Leave the loop */
}
if(value > 100) {
printf("Illegal value \n");
continue;
/* Skip to start loop again */
}
/* Process the value read */
/* guaranteed between 1 and 100 */
...;
...;
} /* end while value !=0 */