Goto statement | Unconditional branching statements

Goto statement | Unconditional branching statements

 In C language, some statements are used in which a conditional expression is not used, yet the control of the program goes from one part to another. Using these statements, the programmer can branch out the program without any condition of testing. Such statements are called unconditional branching statements. 

Example:- goto statement 

Goto Statement – A label is required in the Goto statement, so that branching or looping can be clearly detected.  It is mandatory to use colon (:) at the end of this label.  The label must be located immediately before the instruction statement from which the control is to be moved. 

goto statement can also be forward and backward like 

Goto statement | Unconditional branching statements
When the controller checking the computations or data reaches the goto label, it gets the instruction there and then moves to label: leaving the next data and acts according to the instruction it gets there, this action is in the preceding goto statement is . In the backward goto statement, the control leaves the next structure and reaches the back label again and starts to work in cyclic motion accordingly 

Sometimes the goto statement is used at the end of the program, so by ending a program, the controller again starts doing computations by accessing the real data. This type of looping is used to perform digits, square root, money or other programs in a randomized form. 

The goto statement can be explained with the following example in which to find even and odd numbers from the input numbers. 

/*Program to find out even and odd number*/ 

main ( ) 

 int n; 

 Printf ( "Enter any number:'');

 scanf ("%d'' ,&n);

 /* starting of test condition /* 

 if (n%2==0) 

     goto abc;

 Printf ("\n NUMBER IS ODD ''); 

 goto x : /* label for goto statement*/

 Printf ("\n NUMBER IS EVEN''); 

 x : 

getch ( );

} /*end of the program */ 


What is the reason for using goto statement in C language at least - under which conditions is it useful to use goto statement? 

Not using the goto statement in any programming language or using it as little as possible is considered a sign of good programming. There are many reasons behind not using goto statement 

  1. Some compilers create low quality object code when the goto statement is used. 
  2. It is difficult to read and understand the program 
  3. Control of the program goes to any other part of the program without stopping the smooth flow of the statement program (unconditional transfer), which makes it difficult to understand the logic of the program. 
But sometimes it is necessary to use goto statement, such as this statement is used to get out of deep nested loops immediately. With the help of the switch statement, only one loop can come out, but with the help of goto statement, any number of loops can come out immediately.

Post a Comment

0 Comments