C programming tutorial

                  PREVIOUS                                                                                   NEXT CHAPTER

goto statement

 

goto can transfer the program control anywhere in the program.Target destination of this statement is marked by a label.The target label and goto function must appear in th same function.Hence we cant use it to jump from one function to other.

Syntax

goto label ;

.

.

.

label :

label is any name given by programmer .When goto statement (at the top in above syntax)will be encountered by compiler control will goto the label specified (at bottom in above syntax). Compiler will skip in between code and statements after label : will be executed.

Example

b=1;

C :

printf("%d",++b);

if(b<10) goto C;

Explanation of above program

b is initially 1.

printf statement will print 2 .

If condition will check whether b is less then 10.Since it is 2 hence less therefore goto statement will execute .This statement will take control to label i.e C.printf will again execute and print 3.This will be keep going until b becomes 10.



                  PREVIOUS                                                                                   NEXT CHAPTER