break, continue and goto in C


In c, there are control statements which does not need any condition to control the program execution flow. These control statements are called as unconditional control statements. C programming language provides the following unconditional control statements...

  • break
  • continue
  • goto

The above three statements does not need any condition to control the program execution flow.

break statement

In C, the break statement is used to perform the following two things...

  1. break statement is used to terminate switch case statement
  2. break statement is also used to terminate looping statements like while, do-while and for.

When a break statement is encountered inside the switch case statement, the execution control moves out of the switch statement directly. For example consider the following program...

Example Program | Program to perform all arithmetic operations using switch statement.

#include <stdio.h>
#include<conio.h>
void main(){
   int number1, number2, result ;
   char operator;
   clrscr() ;
   printf("Enter any two integer numbers: ") ;
   scanf("%d%d", &number1, &number2) ;
   printf("Please enter any arithmetic operator: ");
   operator = getchar();
   switch(operator)
   {
       case '+': result = number1 + number2 ;
                 printf("Addition = %d", result) ;
                 break;
       case '-': result = number1 - number2 ;
                 printf("Subtraction = %d", result) ;
                 break;
       case '*': result = number1 * number2 ;
                 printf("Multiplication = %d", result) ;
                 break;
       case '/': result = number1 / number2 ;
                 printf("Division = %d", result) ;
                 break;
       case '%': result = number1 % number2 ;
                 printf("Remainder = %d", result) ;
                 break;
       default: printf("\nWrong selection!!!") ;
   }
   getch() ;
}
Output

Enter any two integer numbers: 50 30
Please enter any arithmetic operator: *
Multiplication = 1500

When the break statement is encountered inside the looping statement, the execution control moves out of the looping statements. The break statement execution is as shown in the following figure.

For example, consider the following example program...

Example Program for break statement.

#include <stdio.h>
#include<conio.h>
void main(){
   char ch ;
   clrscr() ;
   do
   {
       printf("Enter Y / N : ") ;
       scanf("%c", &ch) ;
       if(ch == 'Y')
       {
           printf("Okay!!! Repeat again !!!\n") ;
       }
       else
       {
           printf("Okay !!! Breaking the loop !!!") ;
           break ;
       }
   } while( 1 ) ; 
   getch() ;
}
Output

Enter Y / N : Y
Okay!!! Repeat again !!!
Enter Y / N : Y
Okay!!! Repeat again !!!
Enter Y / N : N
Okay !!! Breaking the loop !!!

continue statement

The continue statement is used to move the program execution control to the beginning of looping statement. When continue statement is encountered in a looping statement, the execution control skips the rest of the statements in the looping block and directly jumps to the beginning of the loop. The continue statement can be used with looping statements like while, do-while and for.

When we use continue statement with while and do-while statements the execution control directly jumps to the condition. When we use continue statement with for statement the execution control directly jumps to the modification portion (increment / decrement / any modification) of the for loop. The continue statement execution is as shown in the following figure...

continue statement in c

Example Program | Program to illustrate continue statement.

#include <stdio.h>
#include<conio.h>
void main(){
   int number ;
   clrscr() ;
   while( 1 )
   {
       printf("Enter any integer number: ") ;
       scanf("%d", &number) ;
       if(number%2 == 0)
       {
           printf("Entered number is EVEN!!! Try another number!!!\n") ;
           continue ;
       }
       else
       {
           printf("You have entered ODD number!!! Bye!!!") ;
           exit(0) ;
       }
   }
   getch() ;
}
Output

Enter any integer numbers: 50
Entered number is EVEN!!! Try another number!!!
Enter any integer numbers: 100
Entered number is EVEN!!! Try another number!!!
Enter any integer numbers: 10
Entered number is EVEN!!! Try another number!!!
Enter any integer number: 15
You have entered ODD number!!! Bye!!!

goto statement

The goto statement is used to jump from one line to another line in the program. Using goto statement we can jump from top to bottom or bottom to top. To jump from one line to another line, the goto statement requires a lable. Lable is a name given to the instruction or line in the program. When we use goto statement in the program, the execution control directly jumps to the line with specified lable.

Example Program for goto statement.

#include <stdio.h>
#include<conio.h>
void main(){
   clrscr() ;
   printf("We are at first printf statement!!!\n") ;
   goto last ;
   printf("We are at second printf statement!!!\n") ;
   printf("We are at third printf statement!!!\n") ;
   last: printf("We are at last printf statement!!!\n") ;
   getch() ;
}
Output

We are at first printf statement!!!
We are at last printf statement!!!

MOST IMPORTANT POINTS TO BE REMEMBERED

When we use break, continue and goto statements, we must follow the following...

  • The break is a keyword so it must be used only in lower case letters.
  • The break statement can not be used with if statement.
  • The break statement can be used only in switch case and looping statements.
  • The break statement can be used with if statement, only if that if statement is written inside the switch case or looping statements.
  • The continue is a keyword so it must be used only in lower case letters.
  • The continue statement is used only within looping statements.
  • The continue statement can be used with if statement, only if that if statement is written inside the looping statements.
  • The goto is a keyword so it must be used only in lower case letters.
  • The goto statement must requires a lable.
  • The goto statement can be used with any statement like if, switch, while, do-while and for etc,.