Break Statement in C Programming

When break is encountered inside any loop, control automatically passes to the first statement after the loop. A break is usually associated with an if.

Example: Write a program to determine whether a number is prime or not. A prime number is one, which is divisible only by 1 or itself.

All we have to do to test whether a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. If remainder of any of these divisions is zero, the number is not a prime. If no division yields a zero then the number is a prime number.

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" ) ; } In this program the moment num % i turns out to be zero, (i.e. num is exactly divisible by i) the message “Not a prime number” is printed and the control breaks out of the while loop. There are two ways the control could have reached outside the while loop: 1 . It jumped out because the number proved to be not a prime. 2. The loop came to an end because the value of i became equal to num. When the loop terminates in the second case, it means that there was no number between 2 to num - 1 that could exactly divide num. That is, num is indeed a prime. If this is true, the program should print out the message “Prime number”. The keyword break, breaks the control only from the while in which it is placed. The following is the example to explain this point. main( ) { int i = 1 , j = 1 ; while ( i++ <= 100 ) { while ( j++ <= 200 ) { if ( j == 150 ) break ; else printf ( "%d %d\n", i, j ) ; } } } In this program when j equals 150, break takes the control outside the inner while only, since it is placed inside the inner while. The previous post of the blog deals with nested if statement of IF'S.

Learn complete course of C Programming here.

CONVERSION OF CONSTANTS IN C PROGRAM

PRIORITY OF AR THEMATIC OPERATIONS IN C

OPERATORS ASSOCIATIVITY IN C

IF STATEMENT

MULTIPLE STATEMENTS IN IF

IF AND ELSE

NESTED IF AND ELSE


BREAK

CONTINUE AND DO WHILE IN C LANGUAGE

SWITCH IN C PROGRAMMING

FUNCTIONS IN C PROGRAMMING


Functions and usage in C part two

Coding in C functions


OTHER PROGRAMMING COURSES:

Dot Net Complete Course Part one and two

ASP.NET part one and two

Programming with C and C Sharp

Interview Questions in dot net

asp.net part one part two

Software Testing Complete course part one two and Interview Questions



1 comment:

  1. I would like to tell, to please mantain coding indentation so it is very easy to read for the reader.
    And also its better to give a simple example to describe the break. like


    int main (void)
    {
    int x;

    while (1)
    {
    printf ("enter 0 to break: ");
    scanf ("%d",&x);

    if(x == 0)
    break;
    else
    printf("Did Not Break !\n");
    }
    printf ("OmG! the program broke!");

    return 0;
    }

    And for prime number program, i would like to link a post from my blog describing twin primes, cousin primes and sexy primes. the is_prime() function is one. but before using it read the instruction, it only accepts odd numbers.

    ReplyDelete