Priniting Numbers using Goto Statement in C++


This program prints all the numbers between a given starting and ending value using a goto statement. Here's how the program works:

  • It prompts the user to enter a starting value.
  • It reads the starting value into the variable i.
  • It prompts the user to enter an ending value.
  • It reads the ending value into the variable n.
  • It uses a goto statement to create a loop that prints the value of i and increments it by 1.
  • It checks if i is less than or equal to n. If it is, the loop goes back to the start label and repeats. If it is not, the loop exits.
  • The program ends.

While this program works correctly, using a goto statement is generally not considered good programming practice because it can make the code difficult to read and maintain. A better alternative would be to use a for or while loop to print the numbers.

Source Code

#include<iostream>
using namespace std;
int main()
{
    int i,n;
    cout<<"Enter the number starting number : ";
    cin>>i;
    cout<<"Enter the number ending number : ";
    cin>>n;
    start:
        cout<<"\n"<<i;
    i++;
    if(i<=n)
    {
        goto start;
    }
    return 0;
}


To download raw file Click Here

Output

Enter the number starting number : 5
Enter the number ending number : 10
5
6
7
8
9
10

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples