Break and Continue
Keywords

Break

  • When you have to terminate the loop at certain point ,early exit from the loop you can do it by break statement.
  • C language provides Break keyword which helps to terminate loop or early exit from the loop.
  • Lets see it's Syntax and then
    we will understand it by doing a program.
  • Break statements is used in loop.

Syntax

for(j=0 ; j<=5 ; j++)
{           // body of for loop.
       if(condition)
       {     // body of if statement.
        break;
       }

}

lets  understand  it by one program
Q:Print the number from 1 to 5 and break the loop when the value of variable = 3

#include<stdio.h>
void main()
{
int i;        //variable decleration

for(i=1;i<=5;i++)      //for loop
{
    if(i == 3)  //'==' is a equal to operator,which evaluate true if both  the operants are same. 
    {
        break;      // when value of i will be 3 then this break
                     //  keyword will terminate the loop.
    }
    else           // other wise it will print the value of i.
    {
        printf("%d\n",i);
    }
}
}

Output:

1
2
  • Explanation.
  • when the value of i was 1,as condition was evaluated to true so it came in the body of for loop.
  • Then control went to if block and condition was checked and it evaluated to false, as you know control goes to else block ,so it came in else block and printed the value, after leaving else block value of the variable has incremented by 1.
  • Now ,the value of variable i = 2,condition was checked and evaluated to true, so it came in
    if block and condition checked and it evaluated to false, so it came
    in else block and printed the value of the variable i which is 2 and after
    leaving the else block it incremented the value of variable by 1.
  • Now value of variable i is 3 and condition was checked and evaluated to true and it came in if block but know condition was evaluated to true and break statement got executed and break statement terminated the loop.
  • so, That is why it did not print 3,4,and 5.
  • This how break keyword works ,it breaks the loop when the required condition is met.

Continue

  • Sometime ,in the loop iteration ,a situation comes that you have to skip some statements of current iteration and continue with the next iteration.
  • For doing that C language provides you the continue keyword
  • continue keyword continues to the next iteration with skipping the remaining statements for that iteration.

Syntax

for(j=0 ; j<=5 ; j++)
{           // body of for loop.
       if(condition)
       {     // body of if statement.
        continue;
       }

}

Lets solve the same problem of printing numbers from 1 to 5,but this time we have skip the number 3 and print the remaining numbers

Remember this time loop will not get terminate it will just skip that part where condition is met.

Solution:

#include<stdio.h>
 
viod main()
{
int j;          //decleration of varaible
for(j=0 ; j<=5 ; j++)
{     //body of for loop
       if(j == 3)
      {     // body of if statement.
          continue;       //continue statement  will execute if value of j will become  to 3.
      }
      else
      {
     printf("%d\n",j);
      }
}

}

Output:

1
2
4
5
  • Explanation
  • For value of j=1 , condition got checked and evaluated true and came in the body of
    for loop, inside the loop value of j was checked for the if statement
    and condition was evaluated to false and control went to else block.
  • In else block value of variable j got printed on the screen, an went for the next iteration.
  • Now value of variable j has become 2.for next iteration condition got checked and
    evaluated true and came in the body of for loop inside the loop value of j
    was checked for the if statement and condition was evaluated to false and
    control went to else block.
  • In else block value of variable j got printed on the screen, an went for the next iteration.
  • Now value of variable j has become 3.for next iteration condition got checked and
    evaluated true and came in the body of for loop inside the loop value of
    j was checked for the if statement but for this iteration condition was
    evaluated to true and continue keyword got executed and it skipped the
    else block and went for next iteration, So it did not print the value
    of variable for this iteration.
  • Now value of variable j has become 4.for next iteration condition got checked and
    evaluated true and came in the body of for loop inside the loop value of j
    was checked for the if statement and condition was evaluated to false and
    control went to else block.
  • In else block value of variable j got printed on the screen, an went for the next iteration.
  • Now value of variable j has become 5.for next iteration condition got checked and
    evaluated true and came in the body of for loop inside the loop value of j
    was checked for the if statement and condition was evaluated to false and
    control went to else block.
  • In else block value of variable j got printed on the screen, an went for the next iteration.
  • Now the value of variable j has become 6,so for next iteration condition got checked and
    condition evaluated false and loop ended normally.

see, the below image of break and continue keywords.
See where the control goes when they get executed.








Further Concepts








  • Programs Regarding If-else Statements
  • Program Regarding Nested If - statements
  • Programs Regarding loops   



  Further Topics