Published by: Nuru
Published date: 21 Jun 2021
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:
For Instance, the flowchart explaining the goto statements is below.
Fig: A Flowchart showing the working of the goto statements
For example, let us see a program executing the goto statements program:
Fig: The Program and the output of a program showing the execution of a goto statement's function