goto statement

The goto statement is a jump statement which is sometimes also referred to as unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.

Syntax :

In the above syntax, the first line tells the compiler to go to or jump to the statement marked as a label. Here label is a user-defined identifier which indicates the target statement. The statement immediately followed after 'label:' is the destination statement. The 'label:' can also appear before the 'goto label;' statement in the above syntax.

Below are some examples on how to use goto statement:

Examples:

In this case, we will see a situation similar to as shown in Syntax1 above. Suppose we need to write a program where we need to check if a number is even or not and print accordingly using the goto statement. Below program explains how to do this:

Output:

In this case, we will see a situation similar to as shown in Syntax1 above. Suppose we need to write a program which prints numbers from 1 to 10 using the goto statement. Below program explains how to do this.

Output:

Disadvantages of using goto statement:

  • The use of goto statement is highly discouraged as it makes the program logic very complex.
  • use of goto makes the task of analyzing and verifying the correctness of programs (particularly those involving loops) very difficult.
  • Use of goto can be simply avoided using break and continue statements.