The perfect place for easy learning...

C Programming Language

×

Topics List

Place your ad here

break, continue and goto in C

In c, there are control statements that do 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 do 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 the 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 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:

for in c programming

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.

break statement in c

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:

for in c programming

continue statement

The continue statement is used to move the program execution control to the beginning of the looping statement. When the 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:

for in c programming

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 label. Label is a name given to the instruction or line in the program. When we use a goto statement in the program, the execution control directly jumps to the line with the specified label.

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:

for in c programming

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 require a label.
  • The goto statement can be used with any statement like if, switch, while, do-while, and for, etc.

Place your ad here
Place your ad here