Saturday, December 9, 2017

Break Statement In C

We know loop continues execution of its body statements until the condition is false. If I want to skip some statements inside loop or terminate execution before the loop condition becomes false. How can we handle this issue ?

To solve this issue there are some case control statements in C programming. Here we will discuss about

  • break
  • continue
  • switch

Break Statement

  • Break statement is used to terminate surrounding loop immediately without checking loop condition.
  • To write break statement in any program we use decision making statement ( if - else ).

Syntax of Break Statement : break ;

Example


#include <stdio.h>

int main()
{
    int i;
    for(i = 1; i <= 10; i++)
    {
        if(i > 5)
        {
            break;
        }

        printf("%d \n", i);
    }
    return 0;
}

Output
1
2
3
4
5

Explanation of above program

We know how for loop works.In this case, for loop should work for 10 iteration. Everytime it executes it body statements it checks the condition in the body statement ( i > 5 ) either true or false. For the first 5 times it is false so break statement does not works there, goes to the next statement and executes the print command but in the 6th iteration the condition became true and it terminate the loop though the loop condition ( i <= 10 ) was still true.



Previous Topic

Next Topic



No comments:

Post a Comment