C Programming

place your ad here

The Goto Statement || Control Statements || CSIT Notes

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. This ‘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.

The Goto Statement

Fig: A Flowchart showing the working of the goto statements

 

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

The Goto Statement

The Goto Statement

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

 

If you liked our content The goto Statement, then do not forget to check our other topics, The if-else-if ladder 

Please don’t forget to connect with us through our social media sites. Facebook: Online Notes Nepal, Instagram: @online_notes_nepal, LinkedIn: @onlinenotesnepal, and Twitter:@onlinenotenepal.

advertise here
webtrickshome