break Statement in C

break Statement in C :

  • break statement in c is a jump statement which skips over a part of the code just like a continue statement.
  • The break statement is somewhat different from continue statement as execution of this statement does not suspend the next iteration but the iteration will be terminated from the loop.
  • Execution resumes at the statement immediately following the body of the terminated statement.
  • It is mostly used inside loops (for, while and do while), if-else and switch statement.

Syntax of break :

break;

Working of break Statement :

The following figure explains the working of a break :

break statement in while loop break statement in do while loop
break statement in for loop

How break statement affects if-else, loops, nested loops, switch-case :

Effect of break in if-else statement :

If a break statement appears in a if-else statement, then it causes an exit from both if block and else block. The following code gives you an example of a break using if-else :

int a,b,c,i;
for(i=0; i<2; ++i)
 {
   printf("Enter 2 Numbers :");
   scanf("%d %d",&a,&b);
   if(b==0)
       break;
   else
       c=a/b;
   printf("\nQuotient = %d\n"c);
  }
 printf("Program Over !");
    :
Output :
Enter 2 Numbers : 10 5
Quotient = 2
Enter 2 Numbers : 10 0
Program Over !

Effect of break in Nested Loops :

If a break statement appears in a nested loop structure, then it causes an exit from only the ver

y loop it appears in.

For example,

for(i=0;i<2;++i)
{
  j=0;
  printf("\n Enter a character");
  scanf(%c,&ch);
  printf("\n");
  for(;;)
  {
   printf("%c",ch);
   j++;
   if(j==10)
   break;
 }
printf("\n....");
}

This code inputs a character and prints it 10 times. The inner loop has an infinite loop structure but the break statement terminates it as soon as j becomes 10 and the control comes to the statement following the inner loop which prints a line of dashes.

Effect of break in switch-case :

A break used in a switch statement will affect only the switch i.e. it will terminate only the very switch it appears in. It does not affect any loop the switch happens to be in.

#include<iostream.h>
int main()
   {
   int x=0, ua=0, ub=0, uc=0, fail=0;
   while(x<=5)
       {
       switch(x++)
           {
           case 1: break;
           case 2: ++ua;
                   break;
           case 3: break;
           case 4: ++ub;
                   break;
           case 5: ++uc;
                   break;
           default : ++fail;
           }
        }
   cout<<"ua = "<<ua<<"\t ub = "<<ub <<endl;
   cout<<"uc = "<<uc<<"\t fail = "<<fail ;
   return 0;
   }
Output :
ua = 1      ub = 1
uc = 1      fail = 1

How Value of loop Variable tells whether a loop has executed a break statement or not ?

One can tell weather a loop has executed a break statement by examining the loop variable.

A loop variable remains in scope even after the loop is terminated either on maturation or prematurely. By examine the value of this loop variable, it can be told weather the loop has been terminated prematurely. The following code fragment illustrates it.

:
int i;
for("i=0;i<100;++i")
  {
    :
    if(ch=='q') break;
    :
  }
if(i<100)
  printf("Loop has ended prematurely \n");
else
  printf("Normal Termination has taken place \n");
 :

If the test expression of the loop is still true even after the termination of the loop, the loop has executed a break statement.

     

Incoming search terms:

  • types of attributes in dbms

Leave a Reply