LATEST

How to use goto statement in C / C++

In this tutorial, we will learn about the goto statement in C / C++ and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

In C and C++ programming, the goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program.

Syntax and flowchart of goto statement in C / C++

Let's see the syntax of the goto statement below:

goto label;
... .. ...
... .. ...
... .. ...
label: 
statement;
... .. ...

Here,

  • label is an identifier. When goto label is encountered, the control of program jumps to label and executes the codes below it.
  • statement is the block of code to be executed.

goto 01 PNG

Flowchart of goto statement in C / C++:

goto 02 PNG

Example: The program requires the user to enter a valid age (valid age must be greater than 0).

C++ programming:

#include <iostream>
using namespace std;
int main() {
    label:      
    cout << "\nEnter your age: ";    
    int age;  
    cin >> age;  
    if (age < 1){
        cout<<"\nYour age entered is not valid. Please re-enter!\n";
        goto label;    
    }    
    else   
    {    
        cout << "\nYour age is: " << age <<endl;     
    }         
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
    label:      
    printf("\nEnter your age: ");    
    int age;  
    scanf("%d", &age);  
    if (age < 1){
        printf("\nYour age entered is not valid. Please re-enter!\n");
        goto label;    
    }    
    else   
    {    
        printf( "\nYour age is: %d\n",age);     
    }         
    printf("\n-------------------------------\n");
    printf("This program is posted at learnnc.com");
    return 0;
}

Output:

goto 03 PNG

The goto statement gives the power to jump to any part of a program but, makes the logic of the program complex and tangled.

The goto statement can be replaced in most of C++ program with the use of break and continue statements.

Goto statement in C / C++ with examples

Here, we have two examples with goto statement in C / C++. These are basic examples when using goto statement in C / C++.

Example 1: The program asks the user to enter an even number. If you enter an odd number, you will be asked to re-enter it.

C++ programming:

#include <iostream>
using namespace std;
int main() {
    label:      
    cout << "\nEnter a number: ";    
    int number;  
    cin >> number;  
    if (number % 2 == 1){
        cout<<"\nYour number entered is odd number. Please re-enter!\n";
        goto label;    
    }    
    else   
    {    
        cout << "\nYour number entered is even number."<<endl;     
    }         
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
    label:      
    printf("\nEnter a number: ");    
    int number;  
    scanf("%d", &number);  
    if (number % 2 == 1){
        printf("\nYour number entered is odd number. Please re-enter!\n");
        goto label;    
    }    
    else   
    {    
        printf("\nYour number entered is even number.\n");     
    }         
    printf("\n-------------------------------\n");
    printf("This program is posted at learnnc.com");
    return 0;
}

Output:

goto 04 PNG

Example 2: This program calculates the average of numbers entered by the user. If the user enters a negative number, it ignores the number and calculates the average number entered before it.

C++ programming:

#include <iostream>
using namespace std;
int main() {
    float num, average, sum = 0.0;
    int i, n;

    cout << "Maximum number of inputs: ";
    cin >> n;

    for(i = 1; i <= n; ++i)
    {
        cout << "Enter n" << i << ": ";
        cin >> num;
        
        if(num < 0.0)
        {
           // Control of the program move to jump:
            goto jump;
        } 
        sum += num;
    }
    
  jump:
    average = sum / (i - 1);
    cout << "\nAverage = " << average;
  
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
    float num, average, sum = 0.0;
    int i, n;

    printf("Maximum number of inputs: ");
    scanf("%d", &n);

    for(i = 1; i <= n; ++i)
    {
        printf("Enter n%d: ",i);
        scanf("%f", &num);
        
        if(num < 0.0)
        {
           // Control of the program move to jump:
            goto jump;
        } 
        sum += num;
    }
    
  jump:
    average = sum / (i - 1);
    printf("\nAverage = %0.2f",average);
  
    printf("\n-------------------------------\n");
    printf("This program is posted at learnnc.com");
    return 0;
}

Output:

goto 05 PNG

Cùng chuyên mục:

How to use strings in C / C++

How to use strings in C / C++

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

How to use for loop in C / C++

How to use for loop in C / C++

Top