break
We have already met break in the discussion of the switch statement. It is used to exit from a loop or a switch, control passing to the first statement beyond the loop or a switch. The break command will exit the most immediately surrounding loop regardless of what the conditions of the loop are. Break is useful if we want to exit a loop under special circumstances.break can be used to force an early exit from the loop, or to implement a loop with a test to exit in the middle of the loop body.
break exit form loop or switch.
#include
#include
{
int i, n, prime=1;
// prime is true
printf(”Input natural number :”);
scanf(”%d”, &n);
for( i=2; i<= n-1; i++)
{
if( n % i == 0 )
{ // also possible to state if(!(n % i))
prime =0; // prime is now false
break;
}
}
if( prime )
printf(”%d is prime number !\n”, n);
else
printf(”%d isn’t prime number!\n”, n);
}
It only works within loops where its effect is to force an immediate jump to the loop control statement.If you are executing a loop and hit a continue statement, the loop will stop its current iteration, update itself (in the case of for loops) and begin to execute again from the top. Essentially, the continue statement is done that iteration of the loop ,and continue with the loop without executing whatever code comes after continue.
continue skip 1 iteration of loop.
Example :
int value =0;
for (index=0 ;index < 10;index++ )
{
if (index==4 || index==5)
continue;
value += numbers[index];
}
printf(”Value = %i”,value) ;
