Any loop can be terminated instantly by using a break statement within the loop's body.
When break is encountered, looping stops and program execution picks up with the next statement after the loop's final curly bracket.
#include <stdio.h> int main() /*from w ww . j a v a2 s .c om*/ { int count; count = 0; while(1) { printf("%d, ",count); count = count+1; if( count > 50) break; } putchar('\n'); return(0); }
The while loop is configured to go on forever, but the if statement can stop it.
When the value of count is greater than 50, the break statement is executed and the loop halts.