AppDividend
Latest Code Tutorials

Goto Statement in C++ Example | C++ Goto Statement Program

0

Goto statement in C++ Example | C++ Goto Statement Program is today’s topic. Goto is an unconditional jump that is used to transfer the control of the program to a given label which is needed to be mentioned by the programmer. hA goto statement can be used to jump from any part of the program to the other part within the function. In C++ programming, the goto statement is used for modifying the standard sequence of the tprogram execution by transferring the control to other part of the program.

Goto Statement in C++

The goto statement is an unconditional jump statement used for transferring the control of a program.

A goto statement is the jump statement which is sometimes also referred to as an unconditional jump statement.

A goto statement can be used to jump from anywhere to anywhere within a function.

It allows the program’s execution flow to jump to a specified location within the function.

There are two ways to call the goto statement.

label1:
...
...    		//statements
goto label1;

See the second way.

label2:
...
... 		 //statements
goto label2;

See the code snippet.

int num = 10;
if (num == 10)
{
  goto p;
}
else
{
  goto q;
}
p : cout << "Hello I am 10 and I am even." << endl;
q : cout << "Hello I am not ten !! ''<<endl;

The output is Hello I am 10, and I am even.

Here in the above example, we have used goto in the if the condition that the number is 10 then it will check for the label mentioned in front on the goto keyword ( which is p here) and it will print the statement discussed over there.

Note- There are two ways by which we can syntactically represent goto statements. First is by mentioning goto label_name first and then label_ name: and the other by specifying label_name: first then goto label_name: 

Flow chart of a goto statement

 

Flow chart of a goto statement

Advantages of goto statements

  1. It gives the advantage of the unconditional jump.
  2. It gives the power to alter the typical sequence of the program.

Disadvantages of goto statements

  1. It makes the logic of the program complex.
  2. When we use goto, it makes the task of analyzing the correctness of the program very difficult.
  3. Use of goto can be avoided using the break and continue statements.
  4. In modern programming, the goto statement is considered a harmful construct and a bad programming practice.

Example programs for Goto

Q1- Write program to show the mechanism of goto.

#include <iostream>
using namespace std;

int main()
{
  int a;
  cout << "Enter the number to check if it is odd or even=";
  cin >> a;
  if (a % 2 == 0)
  {
    goto p;
  }
  else
  {
    goto q;
  }
p:
  cout << "The number is even" << endl;

q:
  cout << "The number is odd" << endl;

  return (0);
}

See the output.

 

C++ Goto Statement Program

Write a program using goto where we will print numbers from 1 to 10 and the place of 10 numerically we will print it alphabetically.

#include <iostream>
using namespace std;

int main()
{
  for (int i = 1; i <= 10; i++)

  {
    if (i == 10)
    {
      goto q;
    }
    cout << i << " "
         << "Element=" << i << endl;
  }
q:
  cout << "I am the 10th element = ten";
}

See the output.

 

Goto Statement in C++ Example

Finally, the Goto Statement in C++ Example | C++ Goto Statement Program is over.

Recommended Posts

C++ Do-while Loop Program

C++ While Loop Example

C++ For Loop Example

If-else Statements in C++ Example

C++ Switch Example

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.