Break, continue, and goto in C

      Break, continue, and goto in ‘C’

In c programming language, there are control statements that do not need any condition to control the execution flow of the program. These control statements are called as unconditional control statements. C- language provides us about (3) unconditional control statements.

These are:-

  • break
  • continue
  • goto

The above three statements do not need any condition for controling the program execution flow.

1)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, let us 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:

            Enter any two integer numbers :  10  20

            Please enter any arithmetic operator : Multiplication =  200   

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

For example, let us consider the following example by writing a 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:

            Do you want to repeat – Enter Y / N : Y

            Okay ! ! ! Repeat again ! ! !

            Do you want to repeat – Enter  Y / N :

2)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…

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 number :10

            Entered number is EVEN ! ! ! Try another number ! ! !

            Enter any integer number : 30

            Entered number is EVEN ! ! ! Try another number ! ! !

            Enter any integer number : 55

            You have entered ODD number ! ! !  Bye ! ! !

3)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 ! ! !

Very-Very IMPORTANT POINTS TO BE REMEMBERED

When we use break, continue and goto statements, we must follow the following steps for the Correct execution:-

  • The break is a keyword so it must be used only in lower case letters.
  • The break statement can’t 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,.

Leave a Reply

Your email address will not be published. Required fields are marked *

Contribute
close slider
present

Contribute and Win Exciting Prizes & Gifts



whatsapp

You can also send file via Whatsapp

+91 9155653926

If you have any year ,any subject,any semester question paper and other study materials please send us

अगर आपके पास किसी सेमेस्टर के किसी भी साल और किसी भी विषय का क्वेश्चन पेपर है और कोई अन्य पढाई सामग्री तो उसे हमलोगों को भेजे