The break Statement

·

The break Statement
The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any C loop, control automati- cally passes to the first statement after the loop.
for e.q. The following program is to determine whether a number is prime or not.

Logic:- To test a number is prime or not, divide it successively by all numbers from 2 to one less than itself. If the remainder of any of the divisions is zero, the number is not a prime.
following program implements this logic main( )
{
int num, i;
printf(“Enter a number”);
scanf(“%d”, &num);
i = 2
while (i < = num -1)
{
if (num%i= = 0)
{
printf(“Not a prime number”);
break;
}
i + +
}
if(i = = num)
printf(“Prime number”);
}

About Me

Blog Archive