The Goto Statement

The Goto Statement

Published by: Nuru

Published date: 21 Jun 2021

The Goto Statement Photo

The Goto Statement

The Goto Statement is a jump statement that is sometimes also referred to as an unconditional jump statement. It can jump anywhere in the function. Also, the goto statement transfers the control to the labeled statement somewhere in the current function.

Thus, the general syntax of the goto statements is:

Syntax 1

Goto label;

--------------

--------------

Label;

Statement;

--------------

--------------

 

Syntax 2

Label1;

-----------

-----------

Goto label;

Statement;

------------

-------------

In the above syntaxes, the compiler goes to the statement that we mark as a label. Therefore, the label here is a user-defined valid identifier that indicates the target statement. It is followed by a colon. Whenever there is an encountering of the statement goto label, there is transferring of the statement that is immediately after the label. Thus, the statements immediately followed after the label is the destination statement. These ‘label’ statements sometimes appear before the ‘goto label’ statements function.

Generally, we avoid the use of a goto statement under some conditions. They are as follows:

  • Branching around statements. Also, groups of statements under certain conditions.
  • Jumping to the end of a loop under certain conditions. Thus bypassing the remainder of the during the current pass.
  • Jumping completely out of the loop under certain conditions. And, terminating the execution of the loop.

For Instance, the flowchart explaining the goto statements is below.

Goto statement

Fig: A Flowchart showing the working of the goto statements

 

For example, let us see a program executing the goto statements program:

Goto statement

Goto statement

 

Fig: The Program and the output of a program showing the execution of a goto statement's function